<?php

namespace App\Filament\Resources\Manage\Cooperations\RelationManagers;

use App\Enums\ApprovalStatus;
use App\Enums\RoleEnum;
use App\Filament\Actions\Cheerful\CreateAction;
use App\Filament\Actions\Cheerful\DeleteAction;
use App\Filament\Actions\Cheerful\EditAction;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteBulkAction;
use Filament\Notifications\Notification;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Support\Icons\Heroicon;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Model;

class TaskAssignmentRelationManager extends RelationManager
{
    protected static string $relationship = 'taskAssignment';

    protected static ?string $title = 'Penugasan';

    public static function canViewForRecord(Model $ownerRecord, string $pageClass): bool
    {
        if (auth()->user()->hasRole(RoleEnum::PERUSAHAAN->value)) {
            return $ownerRecord->cooperationMedia()
                ->whereHas('partnerMedia', function ($query) {
                    $query->whereHas('company', function ($query) {
                        $query->where('user_id', auth()->id());
                    });
                })
                ->where('status', ApprovalStatus::ACCEPTED)
                ->exists();
        }

        return true;
    }

    public function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('start_date')
                    ->label('Mulai')
                    ->date('l, d F Y'),

                TextColumn::make('end_date')
                    ->label('Selesai')
                    ->date('l, d F Y'),

                TextColumn::make('task_description')
                    ->label('Deskripsi Tugas')
                    ->limit(50),

                TextColumn::make('report_amount')
                    ->label('Jumlah Laporan'),
            ])
            ->filters([
                //
            ])
            ->headerActions([
                CreateAction::make()
                    ->label('Buat Penugasan')
                    ->visible(function (): bool {
                        $cooperation = $this->getOwnerRecord();

                        return $cooperation->proposal()->accepted()->exists()
                            && ! $cooperation->taskAssignment()->exists();
                    })
                    ->successNotification(
                        Notification::make()
                            ->title('Penugasan Dibuat! 🚀✨')
                            ->body('Tugas baru berhasil ditetapkan. Media terkait akan segera mengetahuinya! 💪')
                            ->success()
                    ),
            ])
            ->recordActions([
                EditAction::make()
                    ->successNotification(
                        Notification::make()
                            ->title('Penugasan Diperbarui! ✅')
                            ->body('Perubahan pada penugasan berhasil disimpan. 👍')
                            ->success()
                    ),

                DeleteAction::make()
                    ->successNotification(
                        Notification::make()
                            ->title('Penugasan Dihapus! 🗑️')
                            ->body('Penugasan telah berhasil dihapus dari sistem. 👋')
                            ->success()
                    ),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make()
                        ->successNotification(
                            Notification::make()
                                ->title('Banyak Penugasan Dihapus! 🗑️👋')
                                ->body('Semua penugasan yang dipilih telah dihapus. 🧹')
                                ->success()
                        ),
                ]),
            ])
            ->emptyStateIcon(Heroicon::ArchiveBoxXMark)
            ->emptyStateDescription('Tidak ada penugasan yang ditugaskan.');
    }
}
