<?php

namespace App\Filament\Construction\Resources\MilestoneResource\Pages;

use App\Filament\Construction\Resources\MilestoneResource;
use App\Filament\Construction\Resources\ProjectResource;
use App\Models\Project;
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;
use Illuminate\Contracts\Support\Htmlable;

class EditMilestone extends EditRecord
{
    public Project $project;

    protected static string $resource = MilestoneResource::class;

    public function mount(int | string $record): void
    {
        $this->project = MilestoneResource::resolveParentProject();

        parent::mount($record);
    }

    public function booted(): void
    {
        if (isset($this->project)) {
            MilestoneResource::$parentProject = $this->project;
        }
    }

    public function getTitle(): string | Htmlable
    {
        return "Edit Milestone — {$this->project->name}";
    }

    public function getBreadcrumbs(): array
    {
        return [
            ProjectResource::getUrl('index') => 'Projects',
            ProjectResource::getUrl('edit', ['record' => $this->project]) => $this->project->name,
            MilestoneResource::getUrl('index', ['project' => $this->project]) => 'Milestones',
            $this->getBreadcrumb(),
        ];
    }

    protected function getHeaderActions(): array
    {
        return [
            Actions\DeleteAction::make(),
        ];
    }

    protected function getRedirectUrl(): string
    {
        return MilestoneResource::getUrl('index', ['project' => $this->project]);
    }
}
