<?php

namespace App\Livewire\Settings;

use App\Models\SchoolSetting;
use App\Models\User;
use App\Services\SchoolSettingService;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Livewire\Component;
use Livewire\WithFileUploads;

use App\Traits\WithToastNotifications;

class SchoolSettingIndex extends Component
{
    use AuthorizesRequests, WithFileUploads, WithToastNotifications;

    // Profil Sekolah
    public string $school_name = '';
    public string $school_name_formal = '';
    public string $school_npsn = '';
    public string $school_address = '';
    public string $school_city = '';
    public string $school_postal_code = '';
    public string $school_phone = '';
    public string $school_email = '';
    public string $school_website = '';

    // Kop Surat
    public string $school_province = '';
    public string $school_dinas = '';
    public string $school_regency = '';

    // Kepala Sekolah
    public string $principal_name = '';
    public string $principal_nip = '';

    // Aplikasi
    public string $app_name = '';

    // File uploads
    public $logoFile = null;
    public $faviconFile = null;
    public $signatureFile = null;

    // Active tab
    public string $activeTab = 'profil';

    public function mount(): void
    {
        $settings = SchoolSetting::getAllCached();

        $this->school_name        = $settings['school_name'] ?? '';
        $this->school_name_formal = $settings['school_name_formal'] ?? '';
        $this->school_npsn        = $settings['school_npsn'] ?? '';
        $this->school_address     = $settings['school_address'] ?? '';
        $this->school_city        = $settings['school_city'] ?? '';
        $this->school_postal_code = $settings['school_postal_code'] ?? '';
        $this->school_phone       = $settings['school_phone'] ?? '';
        $this->school_email       = $settings['school_email'] ?? '';
        $this->school_website     = $settings['school_website'] ?? '';
        $this->school_province    = $settings['school_province'] ?? '';
        $this->school_dinas       = $settings['school_dinas'] ?? '';
        $this->school_regency     = $settings['school_regency'] ?? '';
        $this->principal_name     = $settings['principal_name'] ?? '';
        $this->principal_nip      = $settings['principal_nip'] ?? '';
        $this->app_name           = $settings['app_name'] ?? 'Sistem Absensi Fingerprint';
    }

    public function setTab(string $tab): void
    {
        $this->activeTab = $tab;
    }

    public function saveProfile(SchoolSettingService $service): void
    {
        $this->authorize('create', User::class);

        $this->validate([
            'school_name'        => ['required', 'string', 'max:255'],
            'school_name_formal' => ['required', 'string', 'max:255'],
            'school_address'     => ['nullable', 'string', 'max:500'],
        ]);

        $service->saveMultiple([
            'school_name'        => $this->school_name,
            'school_name_formal' => $this->school_name_formal,
            'school_npsn'        => $this->school_npsn,
            'school_address'     => $this->school_address,
            'school_city'        => $this->school_city,
            'school_postal_code' => $this->school_postal_code,
            'school_phone'       => $this->school_phone,
            'school_email'       => $this->school_email,
            'school_website'     => $this->school_website,
        ]);

        $this->notifySuccess('Profil Sekolah berhasil disimpan!');
    }

    public function saveLetterhead(SchoolSettingService $service): void
    {
        $this->authorize('create', User::class);

        $service->saveMultiple([
            'school_province' => $this->school_province,
            'school_dinas'    => $this->school_dinas,
            'school_regency'  => $this->school_regency,
        ]);

        $this->notifySuccess('Kop Surat berhasil disimpan!');
    }

    public function savePrincipal(SchoolSettingService $service): void
    {
        $this->authorize('create', User::class);

        $service->saveMultiple([
            'principal_name' => $this->principal_name,
            'principal_nip'  => $this->principal_nip,
        ]);

        $this->notifySuccess('Data Kepala Sekolah berhasil disimpan!');
    }

    public function saveAppSettings(SchoolSettingService $service): void
    {
        $this->authorize('create', User::class);

        $this->validate([
            'app_name' => ['required', 'string', 'max:255'],
        ]);

        $service->saveMultiple([
            'app_name' => $this->app_name,
        ]);

        $this->notifySuccess('Pengaturan Aplikasi berhasil disimpan!');
    }

    public function uploadLogo(SchoolSettingService $service): void
    {
        $this->authorize('create', User::class);

        $this->validate([
            'logoFile' => ['required', 'image', 'mimes:png,jpg,jpeg,svg,webp', 'max:2048'],
        ], [
            'logoFile.required' => 'Pilih file logo terlebih dahulu.',
            'logoFile.image' => 'File harus berupa gambar.',
            'logoFile.max' => 'Ukuran file maksimal 2 MB.',
        ]);

        $service->uploadLogo($this->logoFile);
        $this->logoFile = null;

        $this->notifySuccess('Logo Sekolah berhasil diupload!');
    }

    public function deleteLogo(SchoolSettingService $service): void
    {
        $this->authorize('create', User::class);
        $service->deleteFile('school_logo');
        $this->notifySuccess('Logo Sekolah berhasil dihapus.');
    }

    public function uploadFavicon(SchoolSettingService $service): void
    {
        $this->authorize('create', User::class);

        $this->validate([
            'faviconFile' => ['required', 'file', 'mimes:ico,png,jpg,jpeg,svg,webp', 'max:1024'],
        ], [
            'faviconFile.required' => 'Pilih file favicon terlebih dahulu.',
            'faviconFile.max' => 'Ukuran file maksimal 1 MB.',
        ]);

        $service->uploadFavicon($this->faviconFile);
        $this->faviconFile = null;

        $this->notifySuccess('Favicon berhasil diupload!');
    }

    public function deleteFavicon(SchoolSettingService $service): void
    {
        $this->authorize('create', User::class);
        $service->deleteFile('school_favicon');
        $this->notifySuccess('Favicon berhasil dihapus.');
    }

    public function uploadSignature(SchoolSettingService $service): void
    {
        $this->authorize('create', User::class);

        $this->validate([
            'signatureFile' => ['required', 'image', 'mimes:png,jpg,jpeg,webp', 'max:2048'],
        ], [
            'signatureFile.required' => 'Pilih file tanda tangan terlebih dahulu.',
            'signatureFile.image' => 'File harus berupa gambar.',
            'signatureFile.max' => 'Ukuran file maksimal 2 MB.',
        ]);

        $service->uploadSignature($this->signatureFile);
        $this->signatureFile = null;

        $this->notifySuccess('Tanda Tangan Kepala Sekolah berhasil diupload!');
    }

    public function deleteSignature(SchoolSettingService $service): void
    {
        $this->authorize('create', User::class);
        $service->deleteFile('principal_signature');
        $this->notifySuccess('Tanda Tangan berhasil dihapus.');
    }

    public function render()
    {
        $this->authorize('create', User::class);

        $logoUrl = SchoolSetting::getFileUrl('school_logo');
        $faviconUrl = SchoolSetting::getFileUrl('school_favicon');
        $signatureUrl = SchoolSetting::getFileUrl('principal_signature');

        return view('livewire.settings.school-setting-index', [
            'logoUrl' => $logoUrl,
            'faviconUrl' => $faviconUrl,
            'signatureUrl' => $signatureUrl,
        ])->layout('components.layouts.app');
    }
}
