<?php

namespace App\Filament\Resources\Clients\RelationManagers;

use Filament\Schemas\Schema;
use Filament\Forms\Components\Hidden;
use Filament\Forms\Components\TextInput;
use Filament\Tables\Columns\TextColumn;
use Filament\Actions\CreateAction;
use Filament\Actions\EditAction;
use Filament\Actions\DeleteAction;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteBulkAction;
use App\Filament\Resources\Branches\Pages\EditBranch;
use App\Filament\Resources\Clients\ClientResource;
use App\Models\Branch;
use App\Processes\Branch\CreateBranchProcess;
use Filament\Forms;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Support\RawJs;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Validation\Rules\Unique;

class BranchesRelationManager extends RelationManager
{
    protected static string $relationship = 'branches';

    protected static bool $shouldCheckPolicyExistence = true;

    protected CreateBranchProcess $createBranchProcess;

    public function __construct()
    {
        $this->createBranchProcess = new CreateBranchProcess;
    }

    public function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                Hidden::make('id'),
                TextInput::make('code')->required()
                    ->unique(
                        'branches',
                        'code',
                        ignoreRecord: true,
                        modifyRuleUsing: fn (Unique $rule) => $rule->where('client_id', $this->getOwnerRecord()->id)
                    ),
                TextInput::make('series')->label('Current Series')
                    ->required()
                    ->numeric()
                    ->integer()
                    ->maxLength(6)
                    ->minLength(1)
                    ->mask(RawJs::make(<<<'JS'
                         '999999'
                    JS)),
            ])->columns(1);
    }

    public function table(Table $table): Table
    {
        return $table
            ->recordTitleAttribute('client_id')
            ->columns([
                TextColumn::make('code')->label('Branch Code'),
                TextColumn::make('current_series')->label('Current Series'),
            ])
            ->filters([
                //
            ])
            ->headerActions([
                CreateAction::make()
                    ->mutateDataUsing(fn ($data) => $this->appendCientId($data))
                    ->using(fn ($data) => $this->saveBranch($data)),
            ])
            ->recordActions([
                //                Tables\Actions\ViewAction::make()->url(fn ($record) => EditBranch::getUrl(['record' => $record->id])),
                EditAction::make()
                    ->fillForm(fn ($record) => ['id' => $record->id, 'code' => $record->code, 'series' => $record->current_series])
                    ->mutateDataUsing(fn ($data) => $this->appendCientId($data))
                    ->using(fn ($data) => $this->saveBranch($data))
                    ->url(fn ($record) => EditBranch::getUrl(['record' => $record->id])),
                DeleteAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ]);
    }

    public function appendCientId($data): array
    {
        $data['client_id'] = $this->ownerRecord->id;

        return $data;
    }

    public function saveBranch($data): Branch
    {
        return ClientResource::saveBranch($data);
    }
}
