<?php

namespace App\Filament\Resources\Manage\Cooperations\Actions\Media;

use App\Enums\ApprovalStatus;
use App\Enums\CooperationStatus;
use App\Filament\Resources\Manage\Cooperations\CooperationResource;
use App\Filament\Support\CheerfulNotification;
use App\Models\CooperationMedia;
use App\Notifications\BroadcastNotification;
use Filament\Actions\Action;
use Filament\Forms\Components\SpatieMediaLibraryFileUpload;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Support\Enums\Width;
use Filament\Support\Icons\Heroicon;
use Filament\Support\RawJs;

class PayAction extends Action
{
    protected function setUp(): void
    {
        parent::setUp();

        $this->label('Bayar')
            ->icon(Heroicon::CreditCard)
            ->color('success')
            ->visible(fn (CooperationMedia $record): bool => $record->cooperation->status === CooperationStatus::PAYMENT && ! $record->payment()->exists() && $record->status === ApprovalStatus::ACCEPTED)
            ->schema([
                TextInput::make('amount')
                    ->label('Nominal Pembayaran')
                    ->placeholder('1,000,000')
                    ->autocomplete(false)
                    ->autofocus()
                    ->required()
                    ->mask(RawJs::make('$money($input)'))
                    ->prefix('Rp'),

                SpatieMediaLibraryFileUpload::make('payment_proof')
                    ->label('Bukti Pembayaran')
                    ->disk(config('filesystems.default'))
                    ->acceptedFileTypes(['image/*'])
                    ->maxSize(1024 * 3)
                    ->collection('cooperations')
                    ->customProperties(fn (): array => [
                        'feature' => 'cooperations',
                        'date' => now()->toDateString(),
                        'doc_type' => 'payment-proof',
                    ])
                    ->image()
                    ->required(),

                Textarea::make('description')
                    ->label('Keterangan')
                    ->placeholder('...')
                    ->autocomplete(false),
            ])
            ->action(function (CooperationMedia $record, array $data): void {
                $record->payment()->create([
                    'amount' => str_replace(['Rp ', ','], '', $data['amount']),
                    'description' => $data['description'],
                    'payment_date' => now(),
                ]);

                CheerfulNotification::success(
                    'Pembayaran Berhasil! 💸✨',
                    "Pembayaran untuk {$record->partnerMedia->name} telah berhasil dicatat."
                )->send();

                $partnerUser = $record->partnerMedia->company?->user;
                if ($partnerUser) {
                    $partnerUser->notify(new BroadcastNotification([
                        'title' => 'Pembayaran Telah Dikirim! 💸✨',
                        'body' => "Halo! Pembayaran untuk kerja sama \"{$record->cooperation->title}\" telah berhasil dikirim oleh Admin. Silakan cek detailnya ya! 😊",
                        'action' => [
                            Action::make('view')
                                ->label('Lihat')
                                ->url(CooperationResource::getUrl('view', ['record' => $record->cooperation_id, 'relation' => 0])),
                        ],
                    ]));
                }
            })
            ->modalWidth(Width::Large);
    }
}
