<?php

namespace App\Filament\Resources\Clients\RelationManagers;

use Filament\Schemas\Schema;
use Filament\Actions\Action;
use App\Filament\Resources\Transmittals\TransmittalResource;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Model;

class TransmittalsRelationManager extends RelationManager
{
    protected static string $relationship = 'transmittals';

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

    public function form(Schema $schema): Schema
    {
        return TransmittalResource::form($schema)
            ->fill(
                ['client_id' => $this->getOwnerRecord()->id]
            );
    }

    public function table(Table $table): Table
    {
        return TransmittalResource::table($table)->headerActions([
            Action::make('New Transmittal')->action('openCreateForm'),
        ]);
    }

    public function openCreateForm()
    {
        return redirect()->route('filament.admin.resources.transmittals.create');
    }
}
