<?php

namespace Tests\Feature\Forms;

use App\Domains\Auth\Models\User;
use App\Domains\Authorization\Enums\RoleCode;
use App\Domains\Purchase\Models\Purchase;
use App\Domains\Tenant\Models\Tenant;
use App\Domains\Tenant\Models\WebsiteType;
use Database\Seeders\AuthorizationSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;

class AdminAppSaleFormControllerTest extends TestCase
{
    use RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();

        $this->seed(AuthorizationSeeder::class);
        WebsiteType::query()->create([
            'codigo' => 'onticket',
            'nombre' => 'OnTicket',
        ]);
    }

    public function test_authentication_is_required(): void
    {
        $this->getJson('/api/v1/adminapp/forms/sale')->assertUnauthorized();
    }

    public function test_an_adminapp_user_can_get_the_sale_form(): void
    {
        $tenant = Tenant::query()->create([
            'codigo' => 'acme',
            'nombre' => 'Acme',
            'dominio' => 'acme.test',
            'website_type_code' => 'onticket',
        ]);
        Sanctum::actingAs(User::factory()->create([
            'rol_codigo' => RoleCode::AdminApp->value,
            'tenant_codigo' => $tenant->codigo,
        ]));

        $this->getJson('/api/v1/adminapp/forms/sale')
            ->assertOk()
            ->assertJsonCount(count(Purchase::statuses()), 'data.statuses')
            ->assertJsonPath('data.statuses.0.code', Purchase::STATUS_CREATED)
            ->assertJsonPath('data.statuses.0.name', 'Creada')
            ->assertJsonPath('data.statuses.1.code', Purchase::STATUS_PENDING_PAYMENT)
            ->assertJsonPath('data.statuses.2.code', Purchase::STATUS_IN_REVIEW)
            ->assertJsonPath('data.statuses.3.code', Purchase::STATUS_PAID)
            ->assertJsonPath('data.statuses.6.code', Purchase::STATUS_EXPIRED);
    }

    public function test_a_customer_cannot_get_the_sale_form(): void
    {
        Sanctum::actingAs(User::factory()->create([
            'rol_codigo' => RoleCode::User->value,
            'tenant_codigo' => null,
        ]));

        $this->getJson('/api/v1/adminapp/forms/sale')->assertForbidden();
    }
}
