<?php

namespace App\Filament\Resources\SubmissionResource\Pages;

use App\Filament\Resources\SubmissionResource;
use Filament\Actions;
use Filament\Infolists\Components\Section;
use Filament\Infolists\Components\TextEntry;
use Filament\Infolists\Infolist;
use Filament\Resources\Pages\ViewRecord;
use Illuminate\Support\Facades\Storage;

class ViewSubmission extends ViewRecord
{
    protected static string $resource = SubmissionResource::class;

    public function infolist(Infolist $infolist): Infolist
    {
        return $infolist
            ->schema([
                Section::make('Submission Details')
                    ->schema([
                        TextEntry::make('submission_code')->label('Submission Code'),
                        TextEntry::make('type')->badge(),
                        TextEntry::make('status')->badge(),
                        TextEntry::make('conference_type')->badge(),
                        TextEntry::make('presentation_type')->badge(),
                    ])->columns(3),
                Section::make('Author Information')
                    ->schema([
                        TextEntry::make('author_name'),
                        TextEntry::make('author_email'),
                        TextEntry::make('author_phone'),
                        TextEntry::make('institution'),
                        TextEntry::make('country'),
                        TextEntry::make('co_authors'),
                    ])->columns(2),
                Section::make('Paper Details')
                    ->schema([
                        TextEntry::make('title')->columnSpanFull(),
                        TextEntry::make('sub_theme'),
                        TextEntry::make('keywords')
                            ->badge()
                            ->separator(','),
                        TextEntry::make('abstract')->columnSpanFull(),
                    ])->columns(2),
                Section::make('Uploaded Files')
                    ->schema([
                        TextEntry::make('abstract_file')
                            ->label('Abstract File')
                            ->formatStateUsing(fn ($state) => $state ? basename($state) : '—')
                            ->url(fn ($record) => $record->abstract_file ? route('submission.download', [$record->id, 'abstract']) : null)
                            ->openUrlInNewTab()
                            ->icon('heroicon-o-document-text')
                            ->color('info'),
                        TextEntry::make('full_paper_file')
                            ->label('Full Paper File')
                            ->formatStateUsing(fn ($state) => $state ? basename($state) : '—')
                            ->url(fn ($record) => $record->full_paper_file ? route('submission.download', [$record->id, 'paper']) : null)
                            ->openUrlInNewTab()
                            ->icon('heroicon-o-document-arrow-down')
                            ->color('success'),
                    ])->columns(2),
                Section::make('Review & Registration')
                    ->schema([
                        TextEntry::make('reviewer_notes'),
                        TextEntry::make('registration_code')->copyable(),
                        TextEntry::make('is_paid')
                            ->badge()
                            ->formatStateUsing(fn ($state) => $state ? 'Paid' : 'Unpaid')
                            ->color(fn ($state) => $state ? 'success' : 'warning'),
                    ])->columns(3),
            ]);
    }

    protected function getHeaderActions(): array
    {
        return [
            Actions\EditAction::make(),
            Actions\Action::make('download_abstract')
                ->label('Download Abstract')
                ->icon('heroicon-o-arrow-down-tray')
                ->color('info')
                ->action(fn () => Storage::disk('public')->download($this->record->abstract_file))
                ->visible(fn () => $this->record->abstract_file !== null),
            Actions\Action::make('download_paper')
                ->label('Download Full Paper')
                ->icon('heroicon-o-document-arrow-down')
                ->color('success')
                ->action(fn () => Storage::disk('public')->download($this->record->full_paper_file))
                ->visible(fn () => $this->record->full_paper_file !== null),
        ];
    }
}
