<?php

namespace Tests\Unit\Support;

use App\Models\Event;
use App\Models\EventPackage;
use App\Models\Package;
use App\Support\WatermarkConfigResolver;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class WatermarkConfigResolverTest extends TestCase
{
    use RefreshDatabase;

    public function test_watermark_is_disabled_when_package_blocks_it(): void
    {
        $package = Package::factory()->create([
            'watermark_allowed' => false,
            'branding_allowed' => true,
        ]);

        $event = Event::factory()->create([
            'settings' => [
                'watermark' => [
                    'mode' => 'custom',
                    'asset' => 'branding/custom.png',
                ],
            ],
        ]);

        EventPackage::create([
            'event_id' => $event->id,
            'package_id' => $package->id,
            'purchased_price' => $package->price,
            'purchased_at' => now(),
        ]);

        $event->load('eventPackages.package');
        $event->setRelation('eventPackage', $event->eventPackages->first());

        $config = WatermarkConfigResolver::resolve($event);

        $this->assertSame('none', $config['type']);
        $this->assertSame('none', $config['policy']);
    }

    public function test_branding_locked_forces_base_watermark(): void
    {
        $package = Package::factory()->create([
            'watermark_allowed' => true,
            'branding_allowed' => false,
        ]);

        $event = Event::factory()->create([
            'settings' => [
                'watermark' => [
                    'mode' => 'custom',
                    'asset' => 'branding/custom.png',
                    'position' => 'top-left',
                ],
            ],
        ]);

        EventPackage::create([
            'event_id' => $event->id,
            'package_id' => $package->id,
            'purchased_price' => $package->price,
            'purchased_at' => now(),
        ]);

        $event->load('eventPackages.package');
        $event->setRelation('eventPackage', $event->eventPackages->first());

        $config = WatermarkConfigResolver::resolve($event);

        $this->assertSame('base', $config['type']);
        $this->assertSame(config('watermark.base.asset'), $config['asset']);
        $this->assertSame(config('watermark.base.position'), $config['position']);
    }

    public function test_custom_watermark_is_returned_when_allowed(): void
    {
        $package = Package::factory()->create([
            'watermark_allowed' => true,
            'branding_allowed' => true,
        ]);

        $event = Event::factory()->create([
            'settings' => [
                'watermark' => [
                    'mode' => 'custom',
                    'asset' => 'branding/custom.png',
                    'position' => 'top-center',
                    'opacity' => 0.8,
                    'scale' => 0.5,
                    'padding' => 24,
                    'offset_x' => 10,
                    'offset_y' => -5,
                ],
            ],
        ]);

        EventPackage::create([
            'event_id' => $event->id,
            'package_id' => $package->id,
            'purchased_price' => $package->price,
            'purchased_at' => now(),
        ]);

        $event->load('eventPackages.package');
        $event->setRelation('eventPackage', $event->eventPackages->first());

        $config = WatermarkConfigResolver::resolve($event);

        $this->assertSame('custom', $config['type']);
        $this->assertSame('top-center', $config['position']);
        $this->assertSame('branding/custom.png', $config['asset']);
        $this->assertEquals(0.8, $config['opacity']);
        $this->assertEquals(0.5, $config['scale']);
        $this->assertEquals(24, $config['padding']);
        $this->assertEquals(10, $config['offset_x']);
        $this->assertEquals(-5, $config['offset_y']);
    }
}
