<?php

namespace Tests\Feature;

use App\Models\Ticket;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;

/**
 * Système de tickets utilisateur connecté à Gitea :
 * création (issue), suivi (état + commentaires) et réponse.
 */
class TicketTest extends TestCase
{
    use RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();
        config([
            'services.gitea.url' => 'https://gitea.test',
            'services.gitea.token' => 'fake-token',
            'services.gitea.repo' => 'sam/cesizen',
        ]);
    }

    #[Test]
    public function un_invite_ne_peut_pas_acceder_aux_tickets()
    {
        $this->get('/tickets')->assertRedirect('/login');
    }

    #[Test]
    public function creer_un_ticket_cree_une_issue_et_un_enregistrement_local()
    {
        Http::fake([
            '*/issues' => Http::response(['number' => 42], 201),
        ]);
        $user = User::factory()->create();

        $this->actingAs($user)->post('/tickets', [
            'type' => 'Correction',
            'titre' => 'Un bug à corriger',
            'description' => 'Description suffisamment longue du problème.',
        ])->assertRedirect(route('tickets.show', 42));

        $this->assertDatabaseHas('tickets', [
            'user_id' => $user->id,
            'gitea_number' => 42,
            'type' => 'Correction',
        ]);
    }

    #[Test]
    public function l_utilisateur_voit_le_suivi_de_son_ticket()
    {
        Http::fake([
            '*/issues/42/comments' => Http::response([
                ['body' => 'Nous regardons cela.', 'user' => ['login' => 'admin'], 'created_at' => '2026-07-08T10:00:00Z'],
            ], 200),
            '*/issues/42' => Http::response(['number' => 42, 'state' => 'open', 'title' => 'Un bug', 'body' => 'détail'], 200),
        ]);
        $user = User::factory()->create();
        Ticket::create(['user_id' => $user->id, 'gitea_number' => 42, 'title' => 'Un bug', 'type' => 'Correction']);

        $this->actingAs($user)->get('/tickets/42')
            ->assertOk()
            ->assertSee('Nous regardons cela.')
            ->assertSee('En cours');
    }

    #[Test]
    public function on_ne_peut_pas_consulter_le_ticket_d_un_autre()
    {
        $autre = User::factory()->create();
        Ticket::create(['user_id' => $autre->id, 'gitea_number' => 99, 'title' => 'Privé', 'type' => 'Ajout']);

        $this->actingAs(User::factory()->create())->get('/tickets/99')->assertNotFound();
    }

    #[Test]
    public function l_utilisateur_peut_repondre_a_son_ticket()
    {
        Http::fake(['*/issues/42/comments' => Http::response(['id' => 1], 201)]);
        $user = User::factory()->create();
        Ticket::create(['user_id' => $user->id, 'gitea_number' => 42, 'title' => 'Un bug', 'type' => 'Correction']);

        $this->actingAs($user)->post('/tickets/42/reponse', ['message' => 'Merci pour le suivi !'])
            ->assertRedirect(route('tickets.show', 42))
            ->assertSessionHas('success');

        Http::assertSent(fn ($r) => str_contains($r->url(), '/issues/42/comments') && $r->method() === 'POST');
    }
}
