<?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
{
    public static function getDefaultName(): ?string
    {
        return 'accept';
    }

    protected function setUp(): void
    {
        parent::setUp();

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

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

                    CheerfulNotification::success(
                        CheerfulNotification::getByKey('cooperation.offer_accepted'),
                        CheerfulNotification::getByKey('cooperation.offer_accepted_desc')
                    )->send();

                    // Notify Admins
                    User::query()
                        ->superAdmin()
                        ->get()
                        ->each(function ($admin) use ($record, $cooperationMedia): void {
                            $admin->notify(new BroadcastNotification([
                                'title' => CheerfulNotification::getByKey('cooperation.offer_accepted_by_media'),
                                'body' => CheerfulNotification::getByKey('notification.media_accepted_offer', ['cooperationMedia__partnerMedia__name' => $cooperationMedia->partnerMedia->name, 'record__title' => $record->title]),
                                '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.company', function (Builder $query): void {
                        $query->where('user_id', auth()->id());
                    })
                    ->first();

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