<?php

namespace App\Filament\Pages;

use App\Models\SiteSetting;
use BackedEnum;
use Filament\Actions\Action;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Notifications\Notification;
use Filament\Pages\Page;
use Filament\Schemas\Components\Form;
use Filament\Schemas\Components\Tabs;
use Filament\Schemas\Schema;
use Filament\Support\Icons\Heroicon;

class SiteSettingsPage extends Page
{
    protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedCog6Tooth;

    protected static ?string $navigationLabel = 'Sahypa sazlamalary';

    protected static \UnitEnum|string|null $navigationGroup = null;

    protected static ?int $navigationSort = 10;

    protected string $view = 'filament.pages.site-settings-page';

    /** @var array<string, mixed>|null */
    public ?array $data = [];

    public function mount(): void
    {
        $this->data = SiteSetting::pluck('value', 'key')->toArray();
        $this->form->fill($this->data);
    }

    public function form(Schema $schema): Schema
    {
        return $schema
            ->statePath('data')
            ->components([
                Form::make()
                    ->schema([
                        Tabs::make()
                            ->tabs([
                                Tabs\Tab::make('Kesimçiniň hekaýasy')
                                    ->schema([
                                        TextInput::make('butchers_tale_title')
                                            ->label('Bölüm başlygy')
                                            ->required(),
                                        Textarea::make('butchers_tale_body')
                                            ->label('Tekst')
                                            ->rows(5)
                                            ->columnSpanFull(),
                                        FileUpload::make('butchers_tale_image')
                                            ->label('Surat (gollar)')
                                            ->image()
                                            ->disk('public')
                                            ->directory('settings')
                                            ->columnSpanFull(),
                                    ]),
                                Tabs\Tab::make('Footer')
                                    ->schema([
                                        TextInput::make('footer_brand_name')
                                            ->label('Brend ady')
                                            ->required(),
                                        TextInput::make('footer_copyright')
                                            ->label('Awtorlyk hukugy ýazgysy')
                                            ->required()
                                            ->columnSpanFull(),
                                        TextInput::make('footer_address')
                                            ->label('Salgy'),
                                        TextInput::make('footer_email')
                                            ->label('E-poçta')
                                            ->email(),
                                        TextInput::make('footer_phone')
                                            ->label('Telefon')
                                            ->tel(),
                                    ]),
                            ])
                            ->columnSpanFull(),
                    ])
                    ->columnSpanFull(),
            ]);
    }

    public function save(): void
    {
        $state = $this->form->getState();

        foreach ($state as $key => $value) {
            SiteSetting::updateOrCreate(['key' => $key], ['value' => $value]);
        }

        Notification::make()
            ->title('Sazlamalar saklanyldy.')
            ->success()
            ->send();
    }

    protected function getHeaderActions(): array
    {
        return [
            Action::make('save')
                ->label('Saklamak')
                ->action('save'),
        ];
    }
}
