<?php

namespace App\Filament\Resources\Clients\RelationManagers;

use Filament\Schemas\Schema;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\Select;
use Filament\Tables\Columns\TextColumn;
use Filament\Actions\Action;
use Filament\Actions\ExportAction;
use Filament\Actions\CreateAction;
use Filament\Actions\EditAction;
use Filament\Actions\DeleteAction;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteBulkAction;
use App\Commands\Clients\GenerateBaseAccountCommand;
use App\Filament\Exports\ClientAccountsExporter;
use App\Models\Account;
use Filament\Actions\Exports\Enums\ExportFormat;
use Filament\Forms;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables;
use Filament\Tables\Table;

class AccountsRelationManager extends RelationManager
{
    protected static string $relationship = 'accounts';

    public function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                TextInput::make('account')->required(),
                Textarea::make('description')->nullable(),
                Select::make('account_type_id')
                    ->relationship('accountType', 'type')
                    ->required(),
                Select::make('normal_balance')->options(
                    ['debit' => 'Debit', 'credit' => 'Credit']
                )->required(),
            ]);
    }

    public function table(Table $table): Table
    {
        return $table
            ->recordTitleAttribute('client_id')
            ->columns([
                TextColumn::make('account')->description(fn (Account $record): string => $record->description ?? ''),
                TextColumn::make('accountType.type'),
                TextColumn::make('accountType.normal_balance')->label('Normal Balance'),
            ])
            ->filters([
                //
            ])
            ->headerActions([
                 Action::make('generate-base-accounts')
                 ->requiresConfirmation()
                 ->label('Generate Base Accounts')
                 ->action(function () {
                    $client = $this->getOwnerRecord();
                    if (! $client ) {
                        return;
                    }

                    if($client->accounts()->count() > 0) {
                        return;
                    }
                    app(GenerateBaseAccountCommand::class)->execute($client);
                }),
                ExportAction::make('Export Accounts')->exporter(ClientAccountsExporter::class)->formats([ExportFormat::Csv]),
                CreateAction::make()->label('New Account')->icon('heroicon-o-plus')->slideOver(),
            ])
            ->recordActions([
                EditAction::make()->slideOver(),
                DeleteAction::make()->requiresConfirmation(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make()->icon('heroicon-s-trash')->requiresConfirmation(),
                ]),
            ]);
    }
}
