<?php

namespace App\Filament\Pages\Settings;

use App\Filament\Support\CheerfulNotification;
use App\Settings\GeneralSettings;
use BackedEnum;
use BezhanSalleh\FilamentShield\Traits\HasPageShield;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\RichEditor;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Notifications\Notification;
use Filament\Pages\SettingsPage;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;
use Filament\Support\Icons\Heroicon;
use UnitEnum;

class ManageGeneralSettings extends SettingsPage
{
    use HasPageShield;

    protected static string|UnitEnum|null $navigationGroup = 'Pengaturan';

    protected static string|BackedEnum|null $navigationIcon = Heroicon::Cog6Tooth;

    protected static ?string $navigationLabel = 'Umum';

    protected static ?int $navigationSort = 1;

    protected static string $settings = GeneralSettings::class;

    protected static ?string $title = 'Pengaturan Umum';

    public function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                Section::make('Informasi')
                    ->description('Atur informasi untuk ditampilkan di halaman utama.')
                    ->schema([
                        TextInput::make('site_name')
                            ->label('Nama')
                            ->placeholder('Simedkom')
                            ->autofocus()
                            ->autocomplete(false)
                            ->required()
                            ->maxLength(255),

                        Textarea::make('site_description')
                            ->label('Deskripsi')
                            ->placeholder('Aplikasi Sistem Media Komunikasi Kabupaten Purwakarta')
                            ->autocomplete(false)
                            ->required(),

                        Textarea::make('address')
                            ->label('Alamat')
                            ->placeholder('Jl. Laks. Laut RE. Martadinata No.47')
                            ->autocomplete(false)
                            ->rows(3)
                            ->required()
                            ->maxLength(255),

                        TextInput::make('site_email')
                            ->label('Alamat Surel')
                            ->placeholder('diskominfo@purwakartakab.go.id')
                            ->email()
                            ->required()
                            ->maxLength(255)
                            ->autocomplete(false),

                        TextInput::make('site_phone')
                            ->label('Nomor Telepon')
                            ->placeholder('(0264) 200222')
                            ->required()
                            ->maxLength(255)
                            ->autocomplete(false),

                        RichEditor::make('about_us')
                            ->label('Tentang Kami')
                            ->placeholder('...')
                            ->required()
                            ->columnSpanFull(),
                    ])->columnSpan(2),

                Section::make('Lampiran')
                    ->schema([
                        FileUpload::make('site_logo')
                            ->label('Logo')
                            ->required()
                            ->image()
                            ->directory('settings'),

                        FileUpload::make('site_icon')
                            ->label('Icon')
                            ->required()
                            ->image()
                            ->directory('settings'),
                    ])->columnSpan(1),
            ])
            ->columns(3);
    }

    public function getSavedNotification(): ?Notification
    {
        return CheerfulNotification::update()->send();
    }
}
