<?php

namespace App\Filament\Resources;

use App\Filament\Resources\MaterieResource\Pages;
use App\Models\Materie;
use App\Models\Clasa;
use App\Models\Scoala;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Forms\Get;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Support\Facades\Auth;

class MaterieResource extends Resource
{
    protected static ?string $model = Materie::class;
    protected static ?string $navigationIcon = 'heroicon-o-book-open';
    protected static ?string $navigationLabel = 'Materii';
    protected static ?string $pluralModelLabel = 'Materii';
    protected static ?string $modelLabel = 'Materie';

    public static function form(Form $form): Form
    {
        $user = Auth::user();
        $isAdmin = $user?->hasRole('admin');

        return $form
            ->schema([
                Forms\Components\Select::make('scoala_id')
                    ->label('Școala')
                    ->options(Scoala::all()->pluck('nume', 'id'))
                    ->required()
                    ->searchable()
                    ->live()
                    ->afterStateUpdated(fn($state, callable $set) => $set('clasa_id', null))
                    ->visible($isAdmin)
                    ->default($user?->scoala_id),
                Forms\Components\Select::make('clasa_id')
                    ->label('Clasa')
                    ->options(function (Get $get) use ($user, $isAdmin) {
                        $scoalaId = $isAdmin ? $get('scoala_id') : $user?->scoala_id;
                        return Clasa::where('scoala_id', $scoalaId)->pluck('nume', 'id');
                    })
                    ->required()
                    ->searchable()
                    ->live(),
                Forms\Components\TextInput::make('nume')
                    ->label('Materie (ex: Matematică)')
                    ->required(),
                Forms\Components\TextInput::make('profesor')
                    ->label('Nume profesor'),
                Forms\Components\Select::make('user_id')
                    ->label('Cont profesor')
                    ->options(fn() => \App\Models\User::role('profesor')->pluck('name', 'id'))
                    ->searchable()
                    ->nullable(),
                Forms\Components\TextInput::make('ore_pe_saptamana')
                    ->label('Ore pe săptămână')
                    ->numeric()
                    ->default(2)
                    ->required(),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('clasa.scoala.nume')
                    ->label('Școala')
                    ->searchable()
                    ->sortable()
                    ->visible(Auth::user()?->hasRole('admin')),
                Tables\Columns\TextColumn::make('clasa.nume')
                    ->label('Clasa')
                    ->searchable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('nume')
                    ->label('Materie')
                    ->searchable(),
                Tables\Columns\TextColumn::make('profesor')
                    ->label('Profesor')
                    ->searchable(),
                Tables\Columns\TextColumn::make('ore_pe_saptamana')
                    ->label('Ore/săpt.')
                    ->sortable(),
            ])
            ->actions([
                Tables\Actions\EditAction::make()
                    ->visible(fn() => Auth::user()?->hasAnyRole(['admin', 'director'])),
                Tables\Actions\DeleteAction::make()
                    ->visible(fn() => Auth::user()?->hasAnyRole(['admin', 'director'])),
            ])
            ->bulkActions([]);
    }

    public static function getEloquentQuery(): \Illuminate\Database\Eloquent\Builder
    {
        $query = parent::getEloquentQuery();
        $user = Auth::user();

        if ($user?->hasRole('profesor')) {
            $query->where('user_id', $user->id);
        }

        return $query;
    }

    public static function getRelations(): array
    {
        return [];
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListMateries::route('/'),
            'create' => Pages\CreateMaterie::route('/create'),
            'edit' => Pages\EditMaterie::route('/{record}/edit'),
        ];
    }
}