<?php

namespace App\Filament\Resources\ClientResource\RelationManagers;

use App\Filament\Resources\BranchResource\Pages\EditBranch;
use App\Filament\Resources\ClientResource;
use App\Models\Branch;
use App\Processes\Branch\CreateBranchProcess;
use Filament\Forms;
use Filament\Forms\Form;
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(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\Hidden::make('id'),
                Forms\Components\TextInput::make('code')->required()
                    ->unique(
                        'branches',
                        'code',
                        ignoreRecord: true,
                        modifyRuleUsing: fn (Unique $rule) => $rule->where('client_id', $this->getOwnerRecord()->id)
                    ),
                Forms\Components\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([
                Tables\Columns\TextColumn::make('code')->label('Branch Code'),
                Tables\Columns\TextColumn::make('current_series')->label('Current Series'),
            ])
            ->filters([
                //
            ])
            ->headerActions([
                Tables\Actions\CreateAction::make()
                    ->mutateFormDataUsing(fn ($data) => $this->appendCientId($data))
                    ->using(fn ($data) => $this->saveBranch($data)),
            ])
            ->actions([
                //                Tables\Actions\ViewAction::make()->url(fn ($record) => EditBranch::getUrl(['record' => $record->id])),
                Tables\Actions\EditAction::make()
                    ->fillForm(fn ($record) => ['id' => $record->id, 'code' => $record->code, 'series' => $record->current_series])
                    ->mutateFormDataUsing(fn ($data) => $this->appendCientId($data))
                    ->using(fn ($data) => $this->saveBranch($data))
                    ->url(fn ($record) => EditBranch::getUrl(['record' => $record->id])),
                Tables\Actions\DeleteAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ]);
    }

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

        return $data;
    }

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