<?php

namespace Tests\Feature\Package;

use App\Models\User;
use App\Models\JobPackage;
use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;

/**
 * Test Package Module
 */
class PackageTest extends TestCase
{
    use RefreshDatabase;

    protected User $ppk;
    protected User $admin;

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

        $this->ppk = User::factory()->create(['role' => 'ppk']);
        $this->admin = User::factory()->create(['role' => 'admin']);
    }

    /** @test */
    public function ppk_can_create_package()
    {
        $this->actingAs($this->ppk);

        $data = [
            'name' => 'Pembangunan Jalan Trans Papua',
            'job_type' => 'konstruksi',
            'sub_classification' => 'SI002',
            'budget' => 850000000,
            'province' => 'Papua',
            'regency' => 'Jayapura',
            'district' => 'Sentani',
            'village' => 'Doyo Baru',
            'duration_days' => 90,
            'description' => 'Pembangunan jalan sepanjang 10 KM',
        ];

        $response = $this->postJson('/api/ppk/packages', $data);
        $response->assertStatus(201);

        $this->assertDatabaseHas('job_packages', [
            'name' => 'Pembangunan Jalan Trans Papua',
            'ppk_id' => $this->ppk->id,
            'status' => JobPackage::STATUS_DRAFT,
        ]);
    }

    /** @test */
    public function ppk_can_submit_package()
    {
        $this->actingAs($this->ppk);

        $package = JobPackage::factory()->create([
            'ppk_id' => $this->ppk->id,
            'status' => JobPackage::STATUS_DRAFT,
        ]);

        $response = $this->postJson("/api/ppk/packages/{$package->id}/submit");
        $response->assertStatus(200);

        $this->assertDatabaseHas('job_packages', [
            'id' => $package->id,
            'status' => JobPackage::STATUS_MATCHING,
        ]);
    }

    /** @test */
    public function non_ppk_cannot_create_package()
    {
        $user = User::factory()->create(['role' => 'contractor']);
        $this->actingAs($user);

        $data = [
            'name' => 'Test Package',
            'job_type' => 'konstruksi',
            'budget' => 100000000,
            'province' => 'Papua',
            'regency' => 'Jayapura',
            'district' => 'Sentani',
            'village' => 'Doyo Baru',
            'duration_days' => 30,
        ];

        $response = $this->postJson('/api/ppk/packages', $data);
        $response->assertStatus(403);
    }

    /** @test */
    public function can_get_package_list()
    {
        $this->actingAs($this->admin);

        JobPackage::factory()->count(10)->create();

        $response = $this->getJson('/api/packages');
        $response->assertStatus(200);
        $response->assertJsonStructure([
            'success',
            'data' => [
                '*' => ['id', 'name', 'budget', 'status', 'status_label']
            ],
            'meta'
        ]);
    }
}