<?php

namespace App\Filament\Resources\Customers; 

use App\Filament\Resources\Customers\Pages;
use App\Models\Customer;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\TextInput;
use Filament\Schemas\Components\Fieldset;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;

use App\Enums\NavigationGroup;

class CustomerResource extends Resource
{
    protected static ?string $model = Customer::class;
    protected static string | \BackedEnum | null $navigationIcon = 'heroicon-o-users';
    protected static string | \UnitEnum | null $navigationGroup = NavigationGroup::CUSTOMER->value;
    protected static ?int $navigationSort = 3;

    public static function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                Section::make('Thông tin định danh')
                    ->columns(2)
                    ->schema([
                        TextInput::make('full_name')->label('Họ và Tên')->required(),
                        TextInput::make('cmnd_cccd')->label('Số CMND / CCCD')->required()->unique(ignoreRecord: true),
                        DatePicker::make('dob')->label('Ngày sinh')->displayFormat('d/m/Y'),
                    ]),
                Section::make('Thông liên lạc')
                    ->columns(2)
                    ->schema([
                        TextInput::make('phone')->label('Số điện thoại')->tel()->required(),
                        TextInput::make('email')->label('Email')->email(),
                    ]),
                Section::make('Địa chỉ chi tiết')
                    ->schema([
                        Fieldset::make('address')
                            ->label('Cấu trúc địa chỉ')->columns(3)
                            ->schema([
                                TextInput::make('address.street')->label('Số nhà, đường')->columnSpan(3),
                                TextInput::make('address.ward')->label('Phường / Xã'),
                                TextInput::make('address.district')->label('Quận / Huyện'),
                                TextInput::make('address.city')->label('Tỉnh / Thành phố'),
                            ]),
                    ]),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('full_name')->label('Họ Tên')->searchable(),
                Tables\Columns\TextColumn::make('cmnd_cccd')->label('CMND/CCCD')->searchable(),
                Tables\Columns\TextColumn::make('phone')->label('Điện thoại'),
                Tables\Columns\TextColumn::make('address.city')->label('Tỉnh/Thành')->sortable(),
            ])
            ->defaultSort('created_at', 'desc');
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListCustomers::route('/'),
            'create' => Pages\CreateCustomer::route('/create'),
            'edit' => Pages\EditCustomer::route('/{record}/edit'),
        ];
    }
}
