<?php

namespace App\Filament\Resources\Customers\RelationManagers;

use Filament\Forms\Components\TextInput;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Schemas\Schema;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Filament\Actions\CreateAction;
use Filament\Actions\EditAction;
use Filament\Actions\DeleteAction;

class AddressesRelationManager extends RelationManager
{
    protected static string $relationship = 'addresses';

    protected static ?string $title = 'Addresses';

    public function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                TextInput::make('country')
                    ->label('Address Type')
                    ->helperText('e.g., Home, Office, Other')
                    ->required()
                    ->maxLength(255),
                TextInput::make('street')
                    ->label('Address Line & Upazila/Thana')
                    ->helperText('Format: Address line, Upazila/Thana (e.g. haragach, Kaunia)')
                    ->required()
                    ->maxLength(255),
                TextInput::make('city')
                    ->label('District')
                    ->required()
                    ->maxLength(255),
                TextInput::make('zip')
                    ->label('Zip / Postal Code')
                    ->required()
                    ->maxLength(50),
                TextInput::make('phone')
                    ->label('Phone Number')
                    ->maxLength(20),
            ]);
    }

    public function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('country')
                    ->label('Address Type')
                    ->searchable(),
                TextColumn::make('street')
                    ->label('Street')
                    ->state(function ($record) {
                        $parts = explode(',', $record->street);
                        return trim($parts[0] ?? '');
                    })
                    ->searchable(),
                TextColumn::make('upazila')
                    ->label('Thana/Upazila')
                    ->state(function ($record) {
                        $parts = explode(',', $record->street);
                        return isset($parts[1]) ? trim($parts[1]) : '-';
                    }),
                TextColumn::make('city')
                    ->label('District')
                    ->searchable(),
                TextColumn::make('zip')
                    ->label('Zip')
                    ->searchable(),
                TextColumn::make('phone')
                    ->label('Phone')
                    ->searchable(),
            ])
            ->headerActions([
                CreateAction::make()
                    ->label('New address'),
            ])
            ->actions([
                EditAction::make(),
                DeleteAction::make(),
            ]);
    }
}
