<?php

namespace App\Filament\Association\Resources\ProjectResource\Pages;

use App\Filament\Association\Resources\ProjectResource;
use App\Models\FinancialItem;
use App\Models\Project;
use Filament\Actions;
use Filament\Forms;
use Filament\Resources\Pages\EditRecord;

class EditProject extends EditRecord
{
    protected static string $resource = ProjectResource::class;

    protected function getHeaderActions(): array
    {
        return [
            Actions\Action::make('add_levy')
                ->label('Add Mandatory Levy')
                ->icon('heroicon-o-banknotes')
                ->color('danger')
                ->form([
                    Forms\Components\TextInput::make('title')
                        ->required()
                        ->default(fn () => $this->record->title . ' - Levy'),
                    Forms\Components\TextInput::make('amount')
                        ->required()
                        ->numeric()
                        ->prefix('NGN')
                        ->minValue(100),
                    Forms\Components\Textarea::make('description')
                        ->maxLength(500),
                ])
                ->action(function (array $data) {
                    FinancialItem::create([
                        'association_id' => $this->record->association_id,
                        'title' => $data['title'],
                        'description' => $data['description'] ?? null,
                        'category' => FinancialItem::CATEGORY_LEVY,
                        'amount' => $data['amount'],
                        'is_optional' => false,
                        'is_recurring' => false,
                        'requires_manual_assignment' => false,
                        'is_active' => true,
                        'related_type' => Project::class,
                        'related_id' => $this->record->id,
                    ]);

                    $this->refreshFormData(['linked_items_table']);
                }),

            Actions\Action::make('add_donation')
                ->label('Add Voluntary Donation')
                ->icon('heroicon-o-heart')
                ->color('success')
                ->form([
                    Forms\Components\TextInput::make('title')
                        ->required()
                        ->default(fn () => $this->record->title . ' - Donation'),
                    Forms\Components\TextInput::make('amount')
                        ->label('Suggested Amount')
                        ->numeric()
                        ->prefix('NGN')
                        ->minValue(0)
                        ->helperText('Minimum suggested amount. Members can donate any amount above this.'),
                    Forms\Components\Textarea::make('description')
                        ->maxLength(500),
                ])
                ->action(function (array $data) {
                    FinancialItem::create([
                        'association_id' => $this->record->association_id,
                        'title' => $data['title'],
                        'description' => $data['description'] ?? null,
                        'category' => FinancialItem::CATEGORY_DONATION,
                        'amount' => $data['amount'] ?? 0,
                        'is_optional' => true,
                        'is_recurring' => false,
                        'requires_manual_assignment' => true,
                        'is_active' => true,
                        'related_type' => Project::class,
                        'related_id' => $this->record->id,
                    ]);

                    $this->refreshFormData(['linked_items_table']);
                }),

            Actions\DeleteAction::make(),
        ];
    }
}
