<?php

namespace App\Filament\Resources\FosteringApplications\Pages;

use App\Filament\Resources\FosteringApplications\FosteringApplicationResource;
use App\Filament\Resources\FosteringApplications\Widgets\ApplicantDetailsWidget;
use App\Filament\Resources\FosteringApplications\Widgets\ApplicationAnswersWidget;
use App\Filament\Resources\FosteringApplications\Widgets\NotesWidget;
use App\Mail\FosteringApplicationApproved;
use App\Mail\FosteringApplicationRejected;
use App\Models\FosteringApplication;
use App\Models\Setting;
use Filament\Actions\Action;
use Filament\Forms\Components\Textarea;
use Filament\Resources\Pages\EditRecord;
use Filament\Support\Icons\Heroicon;
use Illuminate\Support\Facades\Mail;

class EditFosteringApplication extends EditRecord
{
    protected static string $resource = FosteringApplicationResource::class;

    public function getRecord(): FosteringApplication
    {
        /** @var FosteringApplication $record */
        $record = parent::getRecord();

        return $record;
    }

    public function getHeading(): string
    {
        return "Fostering Application - {$this->getRecord()->user->name}";
    }

    protected function getHeaderActions(): array
    {
        return [
            Action::make('approve')
                ->label('Approve')
                ->icon(Heroicon::OutlinedCheckCircle)
                ->color('success')
                ->visible(fn () => $this->getRecord()->status === 'submitted')
                ->requiresConfirmation()
                ->modalHeading('Approve Fostering Application')
                ->modalDescription(fn () => 'Are you sure you want to approve this fostering application? '
                    .(Setting::get('email_fostering_application_approved_enabled', true)
                        ? 'An approval email will be sent to the applicant.'
                        : 'NO email will be sent to the applicant.'))
                ->action(function () {
                    $this->getRecord()->update(['status' => 'approved']);

                    if (Setting::get('email_fostering_application_approved_enabled', true)) {
                        Mail::to($this->getRecord()->user)->send(new FosteringApplicationApproved($this->getRecord()));
                    }

                    $this->redirect($this->getResource()::getUrl('index'));
                }),
            Action::make('reject')
                ->label('Reject')
                ->icon(Heroicon::OutlinedXCircle)
                ->color('danger')
                ->visible(fn () => $this->getRecord()->status === 'submitted')
                ->requiresConfirmation()
                ->modalHeading('Reject Fostering Application')
                ->modalDescription(fn () => 'Are you sure you want to reject this fostering application? '
                    .(Setting::get('email_fostering_application_rejected_enabled', true)
                        ? 'A rejection email will be sent to the applicant.'
                        : 'NO email will be sent to the applicant.'))
                ->action(function () {
                    $this->getRecord()->update(['status' => 'rejected']);

                    if (Setting::get('email_fostering_application_rejected_enabled', true)) {
                        Mail::to($this->getRecord()->user)->send(new FosteringApplicationRejected($this->getRecord()));
                    }

                    $this->redirect($this->getResource()::getUrl('index'));
                }),
            Action::make('place_on_hold')
                ->label('Place on Hold')
                ->icon(Heroicon::OutlinedPause)
                ->color('info')
                ->outlined()
                ->visible(fn () => in_array($this->getRecord()->status, ['submitted', 'approved', 'rejected']))
                ->modalHeading('Place Application on Hold')
                ->modalDescription('This application will be placed on hold. It can be released back to Submitted at any time.')
                ->form([
                    Textarea::make('hold_reason')
                        ->label('Reason for Hold')
                        ->placeholder('Explain why this application is being placed on hold...')
                        ->rows(3),
                ])
                ->action(function () {
                    $this->getRecord()->update(['status' => 'hold']);
                    $this->redirect($this->getResource()::getUrl('index'));
                }),
            Action::make('release_from_hold')
                ->label('Release from Hold')
                ->icon(Heroicon::OutlinedArrowPath)
                ->color('primary')
                ->visible(fn () => $this->getRecord()->status === 'hold')
                ->requiresConfirmation()
                ->modalHeading('Release Application from Hold')
                ->modalDescription('This will return the application to Submitted status so it can be reviewed and actioned.')
                ->action(function () {
                    $this->getRecord()->update(['status' => 'submitted']);
                    $this->redirect($this->getResource()::getUrl('index'));
                }),
            Action::make('archive')
                ->label('Archive')
                ->icon(Heroicon::OutlinedArchiveBox)
                ->color('warning')
                ->outlined()
                ->visible(fn () => in_array($this->getRecord()->status, ['approved', 'rejected', 'hold']))
                ->requiresConfirmation()
                ->modalHeading('Archive Application')
                ->modalDescription('Are you sure you want to archive this application? This will move it to archived status.')
                ->action(function () {
                    $this->getRecord()->update(['status' => 'archived']);
                    $this->redirect($this->getResource()::getUrl('index'));
                }),
        ];
    }

    protected function getRedirectUrl(): string
    {
        return $this->getResource()::getUrl('index');
    }

    protected function getHeaderWidgets(): array
    {
        return [
            ApplicantDetailsWidget::make(['record' => $this->getRecord()]),
            ApplicationAnswersWidget::make(['record' => $this->getRecord()]),
        ];
    }

    protected function getFormActions(): array
    {
        return [];
    }

    protected function getFooterWidgets(): array
    {
        return [
            NotesWidget::make(['record' => $this->getRecord()]),
        ];
    }
}
