<?php

namespace App\Filament\Resources\Branches\RelationManagers;

use Filament\Schemas\Schema;
use Filament\Actions\CreateAction;
use App\Filament\Resources\Expenses\Pages\CreateExpense;
use App\Filament\Resources\Expenses\ExpenseResource;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables\Table;

class ExpenseRelationManager extends RelationManager
{
    protected static string $relationship = 'expenses';

    public function form(Schema $schema): Schema
    {
        return $schema
            ->components(ExpenseResource::getExpenseFormFields());
    }

    public function table(Table $table): Table
    {
        return $table
            ->recordTitleAttribute('branch_id')
            ->columns(ExpenseResource::getTableColumns())
            ->headerActions([
                CreateAction::make()
                    ->mutateDataUsing(
                        fn (array $data): array => app(CreateExpense::class)
                            ->getFormDataMutation($data)
                    ),
            ]);
    }
}
