<?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 App\Filament\Columns\RowIndexColumn;
use App\Filament\Support\CheerfulNotification;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteBulkAction;
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 = 'Media Order';

    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([
                ...RowIndexColumn::make(),

                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 Bukti Tayang'),
            ])
            ->filters([
                //
            ])
            ->headerActions([
                CreateAction::make()
                    ->label('Buat Media Order')
                    ->visible(function (): bool {
                        $cooperation = $this->getOwnerRecord();

                        return $cooperation->proposal()->accepted()->exists()
                            && ! $cooperation->taskAssignment()->exists();
                    })
                    ->successNotification(fn () => CheerfulNotification::success(
                        CheerfulNotification::getByKey('cooperation.assignment_success'),
                        CheerfulNotification::getByKey('cooperation.assignment_set_success')
                    )
                    ),
            ])
            ->recordActions([
                EditAction::make()
                    ->successNotification(fn () => CheerfulNotification::success(
                        CheerfulNotification::getByKey('cooperation.assignment_updated'),
                        CheerfulNotification::getByKey('cooperation.assignment_updated_desc')
                    )
                    ),

                DeleteAction::make()
                    ->successNotification(fn () => CheerfulNotification::success(
                        CheerfulNotification::getByKey('cooperation.assignment_deleted'),
                        CheerfulNotification::getByKey('cooperation.assignment_deleted_desc')
                    )
                    ),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make()
                        ->successNotification(fn () => CheerfulNotification::success(
                            CheerfulNotification::getByKey('cooperation.assignment_success_dihapus'),
                            CheerfulNotification::getByKey('cooperation.bulk_assignment_deleted')
                        )
                        ),
                ]),
            ])
            ->emptyStateIcon(Heroicon::OutlinedArchiveBoxXMark)
            ->emptyStateHeading(fn () => CheerfulNotification::getByKey('cooperation.assignment_empty_heading'))
            ->emptyStateDescription(fn () => CheerfulNotification::getByKey('cooperation.assignment_empty'));
    }
}
