<?php

namespace App\Filament\Pages;

use App\Filament\Resources\Projects\ProjectResource;
use App\Models\Project;
use Filament\Actions\EditAction;
use Filament\Pages\Dashboard;
use Filament\Schemas\Schema;
use Filament\Support\Icons\Heroicon;
use UnitEnum;

class ProjectOverview extends Dashboard
{
    protected static string|UnitEnum|null $navigationGroup = 'Gestão do Projeto';

    protected static ?int $navigationSort = 1;

    protected static ?string $navigationLabel = 'Visão Geral';

    protected static ?string $title = 'Visão Geral';

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

    protected string $view = 'filament.pages.project-overview';

    public Project $project;

    public function mount(): void
    {
        $this->project = Project::query()
            ->with([
                'phases',
                'requirementQuestions' => fn ($query) => $query
                    ->with('assignee')
                    ->open()
                    ->latest(),
                'decisions' => fn ($query) => $query
                    ->orderByDesc('decided_at')
                    ->orderByDesc('id')
                    ->limit(5),
            ])
            ->firstOrFail();
    }

    /**
     * @return array<EditAction>
     */
    protected function getHeaderActions(): array
    {
        return [
            EditAction::make()
                ->label('Editar projeto')
                ->record($this->project)
                ->authorize('update')
                ->schema(fn (Schema $schema): Schema => ProjectResource::form($schema))
                ->after(fn () => $this->project->refresh()),
        ];
    }
}
