<?php

namespace Tests\Unit\Models;

use App\Models\JobListing;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\Feature\Concerns\BuildsFeatureData;
use Tests\TestCase;

class JobListingModelTest extends TestCase
{
    use BuildsFeatureData;
    use DatabaseTransactions;

    public function test_formatted_salary_returns_expected_text_for_each_case(): void
    {
        $rangeJob = $this->createJob([
            'salary_min' => 5000000,
            'salary_max' => 7500000,
        ]);

        $minOnlyJob = $this->createJob([
            'salary_min' => 4000000,
            'salary_max' => null,
        ]);

        $noSalaryJob = $this->createJob([
            'salary_min' => null,
            'salary_max' => null,
        ]);

        $this->assertSame('Rp 5.000.000 - Rp 7.500.000', $rangeJob->formatted_salary);
        $this->assertSame('Rp 4.000.000+', $minOnlyJob->formatted_salary);
        $this->assertSame('Negosiasi', $noSalaryJob->formatted_salary);
    }

    public function test_total_stages_and_active_scope_work_as_expected(): void
    {
        $activeJob = $this->createJob([
            'is_active' => true,
        ]);
        $inactiveJob = $this->createJob([
            'is_active' => false,
        ]);

        $this->createStage($activeJob, [
            'stage_number' => 1,
            'sort_order' => 1,
        ]);
        $this->createStage($activeJob, [
            'stage_number' => 2,
            'sort_order' => 2,
        ]);

        $activeJob->refresh();

        $this->assertSame(2, $activeJob->total_stages);

        $activeIds = JobListing::active()->pluck('id');
        $this->assertTrue($activeIds->contains($activeJob->id));
        $this->assertFalse($activeIds->contains($inactiveJob->id));
    }
}
