<?php

namespace App\Filament\SuperAdmin\Pages;

use App\Filament\Clusters\RareAdmin\RareAdminCluster;
use App\Models\WatermarkSetting;
use App\Services\Audit\SuperAdminAuditLogger;
use Filament\Forms;
use Filament\Notifications\Notification;
use Filament\Pages\Page;
use Filament\Schemas\Schema;

class WatermarkSettingsPage extends Page
{
    protected static null|string|\BackedEnum $navigationIcon = 'heroicon-o-sparkles';

    protected static ?string $cluster = RareAdminCluster::class;

    protected string $view = 'filament.super-admin.pages.watermark-settings-page';

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

    protected static ?int $navigationSort = 20;

    public static function getNavigationGroup(): \UnitEnum|string|null
    {
        return __('admin.nav.branding');
    }

    public $asset = [];

    public string $position = 'bottom-right';

    public float $opacity = 0.25;

    public float $scale = 0.2;

    public int $padding = 16;

    public int $offset_x = 0;

    public int $offset_y = 0;

    public function mount(): void
    {
        $settings = WatermarkSetting::query()->first();

        if ($settings) {
            $this->asset = $settings->asset ? [$settings->asset] : [];
            $this->position = $settings->position;
            $this->opacity = (float) $settings->opacity;
            $this->scale = (float) $settings->scale;
            $this->padding = (int) $settings->padding;
            $this->offset_x = (int) ($settings->offset_x ?? 0);
            $this->offset_y = (int) ($settings->offset_y ?? 0);
        }
    }

    public function form(Schema $schema): Schema
    {
        return $schema->schema([
            Forms\Components\FileUpload::make('asset')
                ->label('Basis-Wasserzeichen (PNG/SVG empfohlen)')
                ->disk('public')
                ->directory('branding')
                ->preserveFilenames()
                ->image()
                ->required(),
            Forms\Components\Select::make('position')
                ->label('Position')
                ->options([
                    'top-left' => 'Oben links',
                    'top-right' => 'Oben rechts',
                    'bottom-left' => 'Unten links',
                    'bottom-right' => 'Unten rechts',
                    'center' => 'Zentriert',
                ])
                ->required(),
            Forms\Components\TextInput::make('opacity')
                ->label('Opacity (0–1)')
                ->numeric()
                ->minValue(0)
                ->maxValue(1)
                ->step(0.05)
                ->default(0.25)
                ->required(),
            Forms\Components\TextInput::make('scale')
                ->label('Skalierung (0–1, relativ zur Bildbreite)')
                ->numeric()
                ->minValue(0.05)
                ->maxValue(1)
                ->step(0.05)
                ->default(0.2)
                ->required(),
            Forms\Components\TextInput::make('padding')
                ->label('Padding (px)')
                ->numeric()
                ->minValue(0)
                ->default(16)
                ->required(),
            Forms\Components\TextInput::make('offset_x')
                ->label('Offset X (px)')
                ->numeric()
                ->minValue(-500)
                ->maxValue(500)
                ->default(0)
                ->required(),
            Forms\Components\TextInput::make('offset_y')
                ->label('Offset Y (px)')
                ->numeric()
                ->minValue(-500)
                ->maxValue(500)
                ->default(0)
                ->required(),
        ])->columns(2);
    }

    public function save(): void
    {
        $this->validate();

        $state = $this->form->getState();
        $asset = $state['asset'] ?? $this->asset;
        if (is_array($asset)) {
            $asset = $asset[0] ?? null;
        }

        $settings = WatermarkSetting::query()->firstOrNew([]);
        $settings->asset = $asset;
        $settings->position = $this->position;
        $settings->opacity = $this->opacity;
        $settings->scale = $this->scale;
        $settings->padding = $this->padding;
        $settings->offset_x = $this->offset_x;
        $settings->offset_y = $this->offset_y;
        $settings->save();

        $changed = $settings->getChanges();

        if ($changed !== []) {
            app(SuperAdminAuditLogger::class)->record(
                'watermark_settings.updated',
                $settings,
                SuperAdminAuditLogger::fieldsMetadata(array_keys($changed)),
                source: static::class
            );
        }

        Notification::make()
            ->title('Wasserzeichen aktualisiert')
            ->success()
            ->send();
    }
}
