<?php

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

use App\Enums\ApprovalStatus;
use App\Enums\RoleEnum;
use App\Filament\Resources\Manage\Cooperations\Actions\Media\PayAction;
use App\Models\CooperationMedia;
use Filament\Actions\ViewAction;
use Filament\Infolists\Components\ImageEntry;
use Filament\Infolists\Components\TextEntry;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Schemas\Components\Grid;
use Filament\Schemas\Schema;
use Filament\Support\Icons\Heroicon;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Model;

class CooperationMediaRelationManager extends RelationManager
{
    protected static ?string $recordTitleAttribute = 'partnerMedia.name';

    protected static string $relationship = 'cooperationMedia';

    protected static ?string $title = 'Media';

    public static function canViewForRecord(Model $ownerRecord, string $pageClass): bool
    {
        return ! auth()->user()->hasRole(RoleEnum::PERUSAHAAN->value);
    }

    public static function getBadge(Model $ownerRecord, string $pageClass): ?string
    {
        return $ownerRecord->cooperationMedia()->count();
    }

    public function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('partnerMedia.name')
                    ->label('Nama')
                    ->searchable(),

                TextColumn::make('payment.amount')
                    ->label('Jumlah Pembayaran')
                    ->money('IDR', decimalPlaces: 0)
                    ->placeholder('Belum ditentukan'),

                TextColumn::make('status')
                    ->label('Status')
                    ->badge(),
            ])
            ->filters([])
            ->headerActions([])
            ->recordActions([
                PayAction::make('pay'),

                ViewAction::make()
                    ->visible(fn (CooperationMedia $record): bool => $record->status === ApprovalStatus::ACCEPTED && $record->payment()->exists()),
            ])
            ->emptyStateIcon(Heroicon::Newspaper)
            ->emptyStateDescription('Tidak ada media yang diikutsertakan.')
            ->defaultSort('created_at', 'desc');
    }

    public function infolist(Schema $infolist): Schema
    {
        return $infolist
            ->components([
                Grid::make(2)
                    ->schema([
                        TextEntry::make('payment.amount')
                            ->label('Nominal')
                            ->money('IDR', decimalPlaces: 0),

                        TextEntry::make('payment.payment_date')
                            ->label('Tanggal Bayar')
                            ->dateTime('l, d F Y'),
                    ]),

                TextEntry::make('payment.description')
                    ->label('Keterangan')
                    ->visible(fn (CooperationMedia $record): bool => $record->description !== null),

                ImageEntry::make('payment_proof')
                    ->label('Bukti Pembayaran')
                    ->getStateUsing(fn (CooperationMedia $record): ?string => optional($record->getMedia('cooperations')->where('custom_properties.doc_type', 'payment-proof')->sortByDesc('created_at')->first())->getPathRelativeToRoot())
                    ->disk(config('filesystems.default')),
            ])
            ->columns(1);
    }
}
