<?php

namespace App\Filament\Resources\ClasaResource\Pages;

use App\Filament\Resources\ClasaResource;
use App\Models\Clasa;
use App\Models\OrarSlot;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\Page;
use Illuminate\Support\Facades\Auth;

class EditOrar extends Page
{
    protected static string $resource = ClasaResource::class;
    protected static string $view = 'filament.pages.edit-orar';
    protected static ?string $title = 'Editează orarul';

    public Clasa $record;

    public function mount(Clasa $record): void
    {
        $user = Auth::user();
        $isDiriginte = $user?->hasRole('diriginte') && $record->diriginte_user_id === $user->id;
        $isAdmin = $user?->hasAnyRole(['admin', 'director', 'secretariat']);

        if (!$isDiriginte && !$isAdmin) {
            abort(403);
        }

        $this->record = $record;
    }

    public function getOrarData(): array
    {
        $slots = OrarSlot::withoutGlobalScopes()
            ->where('clasa_id', $this->record->id)
            ->with('materie')
            ->orderBy('ora_index')
            ->get();

        $zile = ['Luni', 'Marți', 'Miercuri', 'Joi', 'Vineri'];
        $maxOra = $slots->max('ora_index') ?? 0;

        $orar = [];
        foreach ($zile as $zi) {
            for ($i = 0; $i <= $maxOra; $i++) {
                $slot = $slots->where('zi', $zi)->where('ora_index', $i)->first();
                $orar[] = [
                    'zi' => $zi,
                    'ora_index' => $i,
                    'ora_inceput' => $slot?->ora_inceput ?? '',
                    'ora_sfarsit' => $slot?->ora_sfarsit ?? '',
                    'materie' => $slot?->materie?->nume ?? null,
                    'profesor' => $slot?->materie?->profesor ?? null,
                    'slot_id' => $slot?->id ?? null,
                    'materie_id' => $slot?->materie_id ?? null,
                ];
            }
        }

        return [
            'zile' => $zile,
            'maxOra' => $maxOra,
            'slots' => $orar,
            'clasa' => $this->record->nume,
            'finalizat' => $this->record->orar_finalizat,
        ];
    }

    public function swapSlots(string $zi1, int $ora1, string $zi2, int $ora2): void
    {
        $slot1 = OrarSlot::withoutGlobalScopes()
            ->where('clasa_id', $this->record->id)
            ->where('zi', $zi1)
            ->where('ora_index', $ora1)
            ->first();

        $slot2 = OrarSlot::withoutGlobalScopes()
            ->where('clasa_id', $this->record->id)
            ->where('zi', $zi2)
            ->where('ora_index', $ora2)
            ->first();

        if ($slot1 && $slot2) {
            $mat1 = $slot1->materie_id;
            $mat2 = $slot2->materie_id;
            $slot1->update(['materie_id' => $mat2]);
            $slot2->update(['materie_id' => $mat1]);
        } elseif ($slot1 && !$slot2) {
            $slot1->update([
                'zi' => $zi2,
                'ora_index' => $ora2,
            ]);
        } elseif (!$slot1 && $slot2) {
            $slot2->update([
                'zi' => $zi1,
                'ora_index' => $ora1,
            ]);
        }

        Notification::make()
            ->title('Orarul a fost actualizat')
            ->success()
            ->send();
    }
}
