<?php

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

use App\Enums\ApprovalStatus;
use App\Enums\RoleEnum;
use App\Filament\Resources\Manage\Cooperations\CooperationResource;
use App\Filament\Support\CheerfulNotification;
use App\Models\Cooperation;
use App\Models\User;
use App\Notifications\BroadcastNotification;
use Filament\Actions\Action;
use Filament\Support\Icons\Heroicon;
use Illuminate\Database\Eloquent\Builder;

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

        $this->label('Terima')
            ->icon(Heroicon::OutlinedCheck)
            ->color('success')
            ->action(function (Cooperation $record): void {
                $cooperationMedia = $record->cooperationMedia()
                    ->whereHas('partnerMedia', function (Builder $query): void {
                        $query->whereHas('company', function (Builder $subQuery): void {
                            $subQuery->where('user_id', auth()->id());
                        });
                    })
                    ->first();

                if ($cooperationMedia) {
                    $cooperationMedia->update(['status' => ApprovalStatus::ACCEPTED]);

                    CheerfulNotification::success(
                        'Berhasil Diterima! ✅✨',
                        'Mantap! Anda telah menerima penawaran kerja sama ini. Silakan lanjut ke tahap pengajuan proposal ya! 💪'
                    )->send();

                    // Notify Admins
                    User::superAdmin()
                        ->get()
                        ->each(function ($admin) use ($record, $cooperationMedia): void {
                            $admin->notify(new BroadcastNotification([
                                'title' => 'Kabar Baik! Tawaran Diterima ✨🤝',
                                'body' => "Media {$cooperationMedia->partnerMedia->name} baru saja menerima penawaran kerja sama \"{$record->title}\". Siap-siap cek proposal mereka ya! 😊",
                                'action' => [
                                    Action::make('view')
                                        ->label('Lihat')
                                        ->url(CooperationResource::getUrl('view', ['record' => $record->id])),
                                ],
                            ]));
                        });
                }
            })
            ->visible(function (Cooperation $record): bool {
                $user = auth()->user();

                if (! $user || ! $user->hasRole(RoleEnum::PERUSAHAAN->value)) {
                    return false;
                }

                $cooperationMedia = $record->cooperationMedia()
                    ->whereHas('partnerMedia', function (Builder $query): void {
                        $query->whereHas('company', function (Builder $subQuery): void {
                            $subQuery->where('user_id', auth()->id());
                        });
                    })
                    ->first();

                return $cooperationMedia && $cooperationMedia->status === ApprovalStatus::PENDING;
            });
    }
}
