<?php

namespace App\Filament\SuperAdmin\Pages;

use App\Models\WatermarkSetting;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Pages\Page;
use Filament\Notifications\Notification;
use Illuminate\Support\Facades\Storage;

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

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

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

    protected static ?int $navigationSort = 20;

    public ?string $asset = null;
    public string $position = 'bottom-right';
    public float $opacity = 0.25;
    public float $scale = 0.2;
    public int $padding = 16;

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

        if ($settings) {
            $this->asset = $settings->asset;
            $this->position = $settings->position;
            $this->opacity = (float) $settings->opacity;
            $this->scale = (float) $settings->scale;
            $this->padding = (int) $settings->padding;
        }
    }

    public function form(Form $form): Form
    {
        return $form->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(),
        ])->columns(2);
    }

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

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

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