<?php

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

use App\Filament\Resources\Manage\Partners\PartnerResource;
use App\Models\CooperationMedia;
use Filament\Actions\Action;
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 CooperationMediaRelationManager extends RelationManager
{
    protected static string $relationship = 'cooperationMedia';

    protected static ?string $title = 'Media';

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

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

                TextColumn::make('status')
                    ->label('Status')
                    ->badge(),
            ])
            ->filters([])
            ->headerActions([])
            ->recordActions([
                Action::make('view')
                    ->label('Lihat')
                    ->icon(Heroicon::Eye)
                    ->url(
                        fn (CooperationMedia $record): string => PartnerResource::getUrl('view', [
                            'record' => $record->partnerMedia->company->user_id,
                        ])
                    ),
            ])
            ->recordAction(null);
    }
}
