<?php

namespace App\Filament\Resources\ProjectDecisions;

use App\Filament\Resources\ProjectDecisions\Pages\ManageProjectDecisions;
use App\Models\ProjectDecision;
use Filament\Actions\DeleteAction;
use Filament\Actions\EditAction;
use Filament\Forms\Components\DateTimePicker;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
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\Table;

class ProjectDecisionResource extends Resource
{
    protected static ?string $model = ProjectDecision::class;

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

    protected static ?int $navigationSort = 4;

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

    protected static ?string $recordTitleAttribute = 'title';

    public static function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                Select::make('project_id')->label('Projeto')->relationship('project', 'name')->searchable()->preload()->required(),
                TextInput::make('title')->label('Título')->required()->maxLength(255)->columnSpanFull(),
                Textarea::make('description')->label('Descrição')->required()->rows(5)->columnSpanFull(),
                TextInput::make('decided_by')->label('Decidida por')->maxLength(255),
                DateTimePicker::make('decided_at')->label('Decidida em'),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('project.name')->label('Projeto')->searchable()->sortable(),
                TextColumn::make('title')->label('Decisão')->searchable()->sortable(),
                TextColumn::make('decided_by')->label('Decidida por')->searchable(),
                TextColumn::make('decided_at')->label('Decidida em')->dateTime('d/m/Y H:i')->sortable(),
            ])
            ->defaultSort('decided_at', 'desc')
            ->recordActions([
                EditAction::make(),
                DeleteAction::make(),
            ]);
    }

    public static function getNavigationLabel(): string
    {
        return 'Decisões';
    }

    public static function getModelLabel(): string
    {
        return 'decisão do projeto';
    }

    public static function getPluralModelLabel(): string
    {
        return 'decisões do projeto';
    }

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