<?php

namespace Tests\Feature\Tenant;

use App\Models\Coupon;
use App\Models\Promotion;
use App\Models\User;
use Database\Seeders\DatabaseSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class PromotionAndReportsFeatureTest extends TestCase
{
    use RefreshDatabase;

    public function test_company_admin_can_create_promotions_and_coupons(): void
    {
        $this->seed(DatabaseSeeder::class);

        $user = User::where('email', 'admin@barberia-demo.test')->firstOrFail();

        $this->actingAs($user)
            ->post(route('tenant.promotions.store'), [
                'name' => 'Semana de cortes',
                'discount_type' => 'percentage',
                'discount_value' => 15,
                'applies_to_all_services' => 1,
                'is_active' => 1,
            ])
            ->assertRedirect();

        $this->actingAs($user)
            ->post(route('tenant.coupons.store'), [
                'code' => 'bienvenido',
                'discount_type' => 'fixed',
                'discount_value' => 10000,
                'is_active' => 1,
            ])
            ->assertRedirect();

        $this->assertDatabaseHas('promotions', ['name' => 'Semana de cortes']);
        $this->assertDatabaseHas('coupons', ['code' => 'BIENVENIDO', 'company_id' => $user->company_id]);
        $this->assertTrue(Promotion::firstOrFail()->is_active);
        $this->assertTrue(Coupon::firstOrFail()->is_active);
    }

    public function test_reports_page_renders_apexcharts_container(): void
    {
        $this->seed(DatabaseSeeder::class);

        $user = User::where('email', 'admin@barberia-demo.test')->firstOrFail();

        $this->actingAs($user)
            ->get(route('tenant.reports.index'))
            ->assertOk()
            ->assertSee('daily-chart')
            ->assertSee('ApexCharts');
    }
}
