<?php

namespace App\Filament\Pages;

use App\Models\WebSetting;
use BackedEnum;

// Filament Core & Icons
use Filament\Pages\Page;
use Filament\Support\Icons\Heroicon;

// Actions & Notifications
use Filament\Actions\Action;
use Filament\Notifications\Notification;

// Schemas Imports (Layout) - SESUAI PATTERN ANDA
use Filament\Schemas\Schema;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Components\Grid;
use Filament\Schemas\Components\Group;

// Forms Imports (Inputs & Traits)
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\FileUpload;

class ManageWebSettings extends Page implements HasForms
{
    use InteractsWithForms;

    protected string $view = 'filament.pages.manage-web-settings';

    // Standar Navigasi Ikon
    protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedCog6Tooth;

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

    public ?array $data = [];

    // --- PEMBATASAN AKSES ---
    public static function canAccess(): bool
    {
        return auth()->check() && auth()->user()->hasRole(['super_admin', 'Admin']);
    }

    // --- PENGATURAN MENU DINAMIS ---
    public static function getNavigationGroup(): ?string
    {
        return 'Pengguna & Pengaturan';
    }
    public static function getNavigationSort(): ?int
    {
        return 3; // Ganti angkanya (1, 2, 3, dst) sesuai urutan yang diinginkan
    }

    public static function getNavigationLabel(): string
    {
        return 'Pengaturan Web';
    }

    // --- SUNTIK DATA AWAL SAAT HALAMAN DIBUKA ---
    public function mount(): void
    {
        $setting = WebSetting::firstOrCreate([
            'site_name' => 'NgekosYuk',
        ]);

        $this->form->fill($setting->toArray());
    }

    // --- TAMPILAN FORM (Layout 2:1) ---
    // PERBAIKAN: Menggunakan Schema $schema sesuai arsitektur Anda!
    public function form(Schema $schema): Schema
    {
        return $schema
            ->schema([
                Grid::make(3)
                    ->schema([
                        // BAGIAN KIRI: Visual & Nama (Porsi 2 Kolom)
                        Group::make()
                            ->schema([
                                Section::make('Identitas Visual Platform')
                                    ->schema([
                                        TextInput::make('site_name')
                                            ->label('Nama Website')
                                            ->required()
                                            ->columnSpanFull(),

                                        FileUpload::make('site_logo')
                                            ->label('Logo Website (Opsional)')
                                            ->image()
                                            ->directory('settings')
                                            ->maxSize(2048)
                                            ->helperText('Gunakan gambar persegi panjang transparan (PNG).'),
                                    ])->columns(2),
                            ])->columnSpan(['default' => 3, 'md' => 2]),

                        // BAGIAN KANAN: Kontak CS (Porsi 1 Kolom)
                        Group::make()
                            ->schema([
                                Section::make('Informasi Kontak (CS)')
                                    ->description('Data ini ditarik ke halaman depan.')
                                    ->schema([
                                        TextInput::make('whatsapp')
                                            ->label('Nomor WhatsApp')
                                            ->prefix('+62')
                                            ->tel()
                                            ->placeholder('81234567890'),

                                        TextInput::make('email')
                                            ->label('Email Resmi')
                                            ->email(),

                                        TextInput::make('phone')
                                            ->label('Telepon Kantor')
                                            ->tel(),

                                        Textarea::make('address')
                                            ->label('Alamat Lengkap Kantor')
                                            ->rows(3),
                                    ]),
                            ])->columnSpan(['default' => 3, 'md' => 1]),
                    ])
            ])
            ->statePath('data');
    }

    // --- TOMBOL SUBMIT ---
    protected function getFormActions(): array
    {
        return [
            Action::make('save')
                ->label('Simpan Pengaturan')
                ->submit('submit')
                ->color('primary')
                ->icon('heroicon-o-check-circle'),
        ];
    }

    // --- LOGIKA SIMPAN DATA ---
    public function submit(): void
    {
        $setting = WebSetting::first();
        if ($setting) {
            $setting->update($this->form->getState());
        }

        Notification::make()
            ->title('Pengaturan Tersimpan!')
            ->body('Seluruh identitas web dan kontak CS telah berhasil diperbarui.')
            ->success()
            ->send();
    }
}
