<?php

namespace App\Filament\Resources\ClasaResource\Pages;

use App\Filament\Resources\ClasaResource;
use Filament\Actions;
use Filament\Infolists\Components\Section;
use Filament\Infolists\Components\TextEntry;
use Filament\Infolists\Components\Grid;
use Filament\Infolists\Infolist;
use Filament\Resources\Pages\ViewRecord;

class ViewClasa extends ViewRecord
{
    protected static string $resource = ClasaResource::class;

    protected function getHeaderActions(): array
    {
        $user = auth()->user();
        $isSecretariat = $user?->hasAnyRole(['admin', 'director', 'secretariat']);
        $isDiriginte = $user?->hasRole('diriginte') && $this->record->diriginte_user_id === $user?->id;

        return [
            Actions\Action::make('toggleFinalizat')
                ->label(fn() => $this->record->orar_finalizat ? 'Deblochează orarul' : 'Finalizează orarul')
                ->icon(fn() => $this->record->orar_finalizat ? 'heroicon-o-lock-open' : 'heroicon-o-lock-closed')
                ->color(fn() => $this->record->orar_finalizat ? 'warning' : 'success')
                ->requiresConfirmation()
                ->modalHeading(fn() => $this->record->orar_finalizat ? 'Deblochează orarul?' : 'Finalizează orarul?')
                ->modalDescription(fn() => $this->record->orar_finalizat
                    ? 'Orarul va putea fi modificat din generator.'
                    : 'Orarul nu va mai putea fi suprascris din generator. Dirigintele poate face doar ajustări minore.')
                ->action(function () {
                    $this->record->update(['orar_finalizat' => !$this->record->orar_finalizat]);
                    $this->refreshFormData(['orar_finalizat']);
                })
                ->visible($isSecretariat),
            Actions\Action::make('editOrar')
                ->label('Editează orarul')
                ->icon('heroicon-o-pencil-square')
                ->color('info')
                ->url(fn() => ClasaResource::getUrl('edit-orar', ['record' => $this->record->id]))
                ->visible($isDiriginte && $this->record->orarSlots()->exists()),
            Actions\Action::make('importElevi')
                ->label('Import elevi CSV')
                ->icon('heroicon-o-arrow-up-tray')
                ->color('info')
                ->url(fn() => ClasaResource::getUrl('import-elevi', ['clasa_id' => $this->record->id]))
                ->visible($isSecretariat),
            Actions\Action::make('exportPdf')
                ->label('Export catalog PDF')
                ->icon('heroicon-o-document-arrow-down')
                ->color('success')
                ->url(fn() => route('pdf.catalog-clasa', $this->record))
                ->openUrlInNewTab(),
            Actions\Action::make('exportMatricol')
                ->label('Registru matricol PDF')
                ->icon('heroicon-o-document-text')
                ->color('info')
                ->url(fn() => route('pdf.registru-matricol', $this->record))
                ->openUrlInNewTab()
                ->visible($isSecretariat),
            Actions\ActionGroup::make([
                Actions\Action::make('siiirElevi')
                    ->label('SIIIR — Elevi (XML)')
                    ->icon('heroicon-o-arrow-up-tray')
                    ->url(fn() => route('siiir.elevi', $this->record))
                    ->openUrlInNewTab(),
                Actions\Action::make('siiirSituatii')
                    ->label('SIIIR — Situații (XML)')
                    ->icon('heroicon-o-arrow-up-tray')
                    ->url(fn() => route('siiir.situatii', $this->record))
                    ->openUrlInNewTab(),
                Actions\Action::make('siiirCsv')
                    ->label('SIIIR — Export CSV')
                    ->icon('heroicon-o-table-cells')
                    ->url(fn() => route('siiir.csv', $this->record))
                    ->openUrlInNewTab(),
            ])
                ->label('Export SIIIR')
                ->icon('heroicon-o-cloud-arrow-up')
                ->color('gray')
                ->visible($isSecretariat),
            Actions\EditAction::make(),
        ];
    }

    public function infolist(Infolist $infolist): Infolist
    {
        return $infolist
            ->schema([
                Section::make('Detalii clasă')
                    ->schema([
                        Grid::make(2)
                            ->schema([
                                TextEntry::make('scoala.nume')->label('Școala'),
                                TextEntry::make('nume')->label('Clasa'),
                                TextEntry::make('an_scolar')->label('An școlar'),
                                TextEntry::make('tip_perioada')->label('Tip perioadă'),
                                TextEntry::make('diriginte.name')->label('Diriginte'),
                                TextEntry::make('orar_finalizat')
                                    ->label('Orar')
                                    ->formatStateUsing(fn($state) => $state ? 'Finalizat' : 'În lucru')
                                    ->color(fn($state) => $state ? 'success' : 'warning')
                                    ->badge(),
                                TextEntry::make('cu_teze')
                                    ->label('Teze')
                                    ->formatStateUsing(fn($state) => $state ? 'Da (25% din medie)' : 'Nu')
                                    ->color(fn($state) => $state ? 'info' : 'gray')
                                    ->badge(),
                            ]),
                    ]),
            ]);
    }
}