<?php

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;

uses(RefreshDatabase::class);

beforeEach(function (): void {
    $this->artisan('lottery:admin-auth-sync')->assertExitCode(0);
});

test('cannot open duplicate settlement period for same range', function (): void {
    $siteId = (int) DB::table('admin_sites')->where('is_default', true)->value('id');
    $super = \App\Models\AdminUser::query()->create([
        'username' => 'period_dup_super',
        'name' => 'Super',
        'email' => null,
        'password' => \Illuminate\Support\Facades\Hash::make('secret-strong'),
        'status' => 0,
    ]);
    grantSuperAdminRole($super);
    $token = $super->createToken('test', ['*'], now()->addDay())->plainTextToken;

    $body = [
        'admin_site_id' => $siteId,
        'period_start' => '2026-06-01 00:00:00',
        'period_end' => '2026-06-30 23:59:59',
    ];

    $this->withHeader('Authorization', 'Bearer '.$token)
        ->postJson('/api/v1/admin/settlement-periods', $body)
        ->assertCreated();

    $this->withHeader('Authorization', 'Bearer '.$token)
        ->postJson('/api/v1/admin/settlement-periods', $body)
        ->assertStatus(422)
        ->assertJsonPath(
            'data.errors.period_start.0',
            trans('validation.business.period_already_open'),
        );
});

test('cannot open settlement period overlapping a closed period', function (): void {
    $siteId = (int) DB::table('admin_sites')->where('is_default', true)->value('id');
    $super = \App\Models\AdminUser::query()->create([
        'username' => 'period_overlap_super',
        'name' => 'Super',
        'email' => null,
        'password' => \Illuminate\Support\Facades\Hash::make('secret-strong'),
        'status' => 0,
    ]);
    grantSuperAdminRole($super);
    $token = $super->createToken('test', ['*'], now()->addDay())->plainTextToken;

    DB::table('settlement_periods')->insert([
        'admin_site_id' => $siteId,
        'period_start' => '2026-05-01 00:00:00',
        'period_end' => '2026-05-31 23:59:59',
        'status' => 'closed',
        'created_at' => now(),
        'updated_at' => now(),
    ]);

    $this->withHeader('Authorization', 'Bearer '.$token)
        ->postJson('/api/v1/admin/settlement-periods', [
            'admin_site_id' => $siteId,
            'period_start' => '2026-05-15 00:00:00',
            'period_end' => '2026-06-15 23:59:59',
        ])
        ->assertStatus(422)
        ->assertJsonPath(
            'data.errors.period_start.0',
            trans('validation.business.period_overlaps_existing'),
        );
});

test('cannot open second settlement period while another is open on same site', function (): void {
    $siteId = (int) DB::table('admin_sites')->where('is_default', true)->value('id');
    $super = \App\Models\AdminUser::query()->create([
        'username' => 'period_one_open_super',
        'name' => 'Super',
        'email' => null,
        'password' => \Illuminate\Support\Facades\Hash::make('secret-strong'),
        'status' => 0,
    ]);
    grantSuperAdminRole($super);
    $token = $super->createToken('test', ['*'], now()->addDay())->plainTextToken;

    $this->withHeader('Authorization', 'Bearer '.$token)
        ->postJson('/api/v1/admin/settlement-periods', [
            'admin_site_id' => $siteId,
            'period_start' => '2026-06-01 00:00:00',
            'period_end' => '2026-06-07 23:59:59',
        ])
        ->assertCreated();

    $this->withHeader('Authorization', 'Bearer '.$token)
        ->postJson('/api/v1/admin/settlement-periods', [
            'admin_site_id' => $siteId,
            'period_start' => '2026-06-08 00:00:00',
            'period_end' => '2026-06-14 23:59:59',
        ])
        ->assertStatus(422)
        ->assertJsonPath(
            'data.errors.period_start.0',
            trans('validation.business.period_site_has_open'),
        );
});
