<?php

namespace App\Filament\Association\Resources\ElectionResource\RelationManagers;

use App\Models\Election;
use App\Models\ElectionCandidate;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables;
use Filament\Tables\Table;

class PositionsRelationManager extends RelationManager
{
    protected static string $relationship = 'positions';
    protected static ?string $title = 'Positions';

    public function form(Form $form): Form
    {
        return $form->schema([
            Forms\Components\TextInput::make('title')
                ->required()
                ->maxLength(255),
            Forms\Components\Textarea::make('description')
                ->rows(3)
                ->maxLength(2000),
            Forms\Components\Grid::make(3)->schema([
                Forms\Components\TextInput::make('available_seats')
                    ->label('Available Seats')
                    ->numeric()
                    ->minValue(1)
                    ->default(1)
                    ->required(),
                Forms\Components\TextInput::make('max_candidates_per_voter')
                    ->label('Max Selections per Voter')
                    ->numeric()
                    ->minValue(1)
                    ->default(1)
                    ->required(),
                Forms\Components\TextInput::make('display_order')
                    ->label('Display Order')
                    ->numeric()
                    ->minValue(0)
                    ->default(0),
            ]),
            Forms\Components\Select::make('voting_strategy_override')
                ->label('Voting Strategy Override')
                ->options(Election::STRATEGIES)
                ->placeholder('Use election default')
                ->helperText('Override the election-wide voting strategy for this position only'),
            Forms\Components\Toggle::make('is_active')
                ->label('Active')
                ->default(true),
            Forms\Components\Hidden::make('association_id')
                ->default(fn () => filament()->getTenant()?->id),
        ]);
    }

    public function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('title')
                    ->searchable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('available_seats')
                    ->label('Seats')
                    ->sortable(),
                Tables\Columns\TextColumn::make('candidates_count')
                    ->counts('candidates')
                    ->label('Total Candidates'),
                Tables\Columns\TextColumn::make('approved_candidates_count')
                    ->counts('approvedCandidates')
                    ->label('Approved'),
                Tables\Columns\TextColumn::make('voting_strategy_override')
                    ->label('Strategy Override')
                    ->placeholder('Default')
                    ->formatStateUsing(fn (?string $state) => $state ? (Election::STRATEGIES[$state] ?? $state) : null),
                Tables\Columns\IconColumn::make('is_active')
                    ->boolean()
                    ->label('Active'),
                Tables\Columns\TextColumn::make('display_order')
                    ->label('Order')
                    ->sortable(),
            ])
            ->defaultSort('display_order')
            ->reorderable('display_order')
            ->headerActions([
                Tables\Actions\CreateAction::make()
                    ->mutateFormDataUsing(function (array $data): array {
                        $data['association_id'] = filament()->getTenant()?->id;
                        return $data;
                    }),
            ])
            ->actions([
                Tables\Actions\EditAction::make(),
                Tables\Actions\DeleteAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ]);
    }
}
