<?php

namespace App\Filament\Resources\ClientResource\Pages;

use App\Filament\Resources\ClientResource;
use Filament\Actions;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Pages\ManageRelatedRecords;
use Filament\Tables;
use Filament\Tables\Table;
use Filament\Support\Enums\FontWeight;
use Filament\Support\Enums\MaxWidth;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Illuminate\Contracts\Support\Htmlable;

class ManageAddresses extends ManageRelatedRecords
{
    protected static string $resource = ClientResource::class;

    protected static string $relationship = 'addresses';

    protected static ?string $navigationIcon = 'heroicon-m-map';
    protected static ?string $navigationLabel = 'Adres Bilgileri';

    private static ?string $modelLabel = 'Adres';

    public function getTitle(): string | Htmlable
    {
        return self::$navigationLabel;
    }

    public function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\TextInput::make('name')
                    ->label('Başlık')
                    ->placeholder('Ev, Yazlık, İkamet Adresi vb.')
                    ->maxLength(100)
                    ->required(),

                Forms\Components\TextInput::make('city')
                    ->label('İl')
                    ->maxLength(50)
                    ->required(),

                Forms\Components\TextInput::make('district')
                    ->label('İlçe')
                    ->maxLength(50)
                    ->required(),

                Forms\Components\TextInput::make('postal_code')
                    ->label('Posta Kodu')
                    ->maxLength(20),

                Forms\Components\Textarea::make('address')
                    ->label('Açık Adres')
                    ->autosize()
                    ->required(),

                Forms\Components\Toggle::make('is_primary')
                    ->label('Birincil Kullanım Durumu')
                    ->inline(false),
            ])
            ->columns(1);
    }

    public function table(Table $table): Table
    {
        return $table
            // ->recordTitleAttribute('name')
            // ->recordTitle(fn ($record): string => {$record->name})
            // ->heading('Clients')
            // ->description('Manage your clients here.')
            // ->modelLabel(self::$modelLabel)
            ->searchDebounce('1000ms')
            ->columns([
                Tables\Columns\TextColumn::make('name')
                    ->label('Adres')
                    ->description(fn ($record): string => $record->address)
                    ->weight(FontWeight::Medium)
                    ->searchable(),

                Tables\Columns\TextColumn::make('city')
                    ->label('İl'),

                Tables\Columns\TextColumn::make('district')
                    ->label('İlçe'),

                Tables\Columns\ToggleColumn::make('is_primary')
                    ->label('Birincil Kullanım')
                    ->afterStateUpdated(function ($record, $state) {
                        if ($state) {
                            $record->addressable->addresses()
                                ->where('id', '!=', $record->id)
                                ->update(['is_primary' => false]);
                        }
                    }),

                Tables\Columns\TextColumn::make('created_at')
                    ->label('Oluşturma')
                    ->dateTime()
                    ->toggleable(isToggledHiddenByDefault: true),

                Tables\Columns\TextColumn::make('updated_at')
                    ->label('Güncelleme')
                    ->dateTime()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                //
            ])
            ->headerActions([
                Tables\Actions\CreateAction::make()
                    ->modelLabel(self::$modelLabel)
                    ->modalWidth(MaxWidth::Large)
                    ->slideOver()
                    ->createAnother(false)
                    ->after(function ($record) {
                        if ($record->is_primary) {
                            $record->addressable->addresses()
                                ->where('id', '!=', $record->id)
                                ->update(['is_primary' => false]);
                        }
                    }),
            ])
            ->actions([
                Tables\Actions\ActionGroup::make([
                    Tables\Actions\ViewAction::make()
                        ->modalHeading(__('filament-actions::view.single.modal.heading', ['label' => self::$modelLabel]))
                        ->modalWidth(MaxWidth::Large)
                        ->slideOver(),

                    Tables\Actions\EditAction::make()
                        ->modalHeading(__('filament-actions::edit.single.modal.heading', ['label' => self::$modelLabel]))
                        ->modalWidth(MaxWidth::Large)
                        ->slideOver()
                        ->after(function ($record) {
                            if ($record->is_primary) {
                                $record->addressable->addresses()
                                    ->where('id', '!=', $record->id)
                                    ->update(['is_primary' => false]);
                            }
                        }),

                    Tables\Actions\DeleteAction::make()
                        ->modalHeading(__('filament-actions::delete.single.modal.heading', ['label' => self::$modelLabel])),
                ]),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ])
            ->emptyStateHeading(__('filament-tables::table.empty.heading', ['model' => self::$modelLabel]))
            ->emptyStateDescription(__('filament-tables::table.empty.description', ['model' => self::$modelLabel]));
    }
}
