<?php

namespace App\Filament\Resources;

use App\Filament\Resources\MarriageResource\Pages;
use App\Filament\Resources\MarriageResource\RelationManagers;
use App\Models\Marriage;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;

class MarriageResource extends Resource
{
    protected static ?string $model = Marriage::class;

    protected static ?string $navigationIcon = 'heroicon-o-heart';
    protected static ?string $navigationLabel = 'Rekod Perkahwinan';
    protected static ?string $pluralModelLabel = 'Rekod Perkahwinan';
    protected static ?string $modelLabel = 'Perkahwinan';

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\Select::make('husband_id')
                    ->label('Suami (Lelaki)')
                    ->options(\App\Models\User::where('gender', 'Lelaki')->where('id', '!=', 1)->pluck('name', 'id'))
                    ->searchable()
                    ->required(),
                Forms\Components\Select::make('wife_id')
                    ->label('Isteri (Perempuan)')
                    ->options(\App\Models\User::where('gender', 'Perempuan')->where('id', '!=', 1)->pluck('name', 'id'))
                    ->searchable()
                    ->required(),
                Forms\Components\DatePicker::make('marriage_date')
                    ->label('Tarikh Perkahwinan')
                    ->nullable(),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('husband.name')
                    ->label('Suami')
                    ->searchable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('wife.name')
                    ->label('Isteri')
                    ->searchable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('marriage_date')
                    ->label('Tarikh Perkahwinan')
                    ->date('d M Y')
                    ->sortable(),
            ])
            ->filters([
                //
            ])
            ->actions([
                Tables\Actions\EditAction::make(),
                Tables\Actions\DeleteAction::make(),
            ])
            ->bulkActions([
                // Disabled temporarily to prevent intl extension error in XAMPP Mac
            ]);
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ManageMarriages::route('/'),
        ];
    }
}
