<?php

namespace App\Filament\Resources\ProjectPhases;

use App\Filament\Resources\ProjectPhases\Pages\ManageProjectPhases;
use App\Models\ProjectPhase;
use Filament\Actions\DeleteAction;
use Filament\Actions\EditAction;
use Filament\Forms\Components\DateTimePicker;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Resources\Resource;
use Filament\Schemas\Schema;
use Filament\Support\Icons\Heroicon;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Table;

class ProjectPhaseResource extends Resource
{
    protected static ?string $model = ProjectPhase::class;

    protected static string|\UnitEnum|null $navigationGroup = 'Gestão do Projeto';

    protected static ?int $navigationSort = 2;

    protected static string|\BackedEnum|null $navigationIcon = Heroicon::OutlinedRectangleStack;

    protected static ?string $recordTitleAttribute = 'name';

    public static function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                Select::make('project_id')->label('Projeto')->relationship('project', 'name')->searchable()->preload()->required(),
                TextInput::make('name')->label('Nome')->required()->maxLength(100),
                TextInput::make('slug')->label('Slug')->required()->maxLength(100),
                TextInput::make('position')->label('Posição')->numeric()->minValue(1)->required(),
                Select::make('status')->label('Status')->options(ProjectPhase::STATUS_OPTIONS)->default('pending')->required(),
                DateTimePicker::make('started_at')->label('Iniciada em'),
                DateTimePicker::make('completed_at')->label('Concluída em'),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('project.name')->label('Projeto')->searchable()->sortable(),
                TextColumn::make('position')->label('Posição')->sortable(),
                TextColumn::make('name')->label('Fase')->searchable(),
                TextColumn::make('status')->label('Status')->formatStateUsing(fn (string $state): string => ProjectPhase::STATUS_OPTIONS[$state] ?? $state)->badge(),
                TextColumn::make('started_at')->label('Iniciada em')->dateTime('d/m/Y H:i')->sortable(),
                TextColumn::make('completed_at')->label('Concluída em')->dateTime('d/m/Y H:i')->sortable(),
            ])
            ->filters([
                SelectFilter::make('status')->label('Status')->options(ProjectPhase::STATUS_OPTIONS),
            ])
            ->defaultSort('position')
            ->recordActions([
                EditAction::make(),
                DeleteAction::make(),
            ]);
    }

    public static function getNavigationLabel(): string
    {
        return 'Fases';
    }

    public static function getModelLabel(): string
    {
        return 'fase do projeto';
    }

    public static function getPluralModelLabel(): string
    {
        return 'fases do projeto';
    }

    public static function getPages(): array
    {
        return [
            'index' => ManageProjectPhases::route('/'),
        ];
    }
}
