<?php

namespace App\Filament\Resources\ClientResource\RelationManagers;

use App\Filament\Resources\TransmittalResource;
use Filament\Forms\Form;
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(Form $form): Form
    {
        return TransmittalResource::form($form)
            ->fill(
                ['client_id' => $this->getOwnerRecord()->id]
            );
    }

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

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