<?php

namespace Tests\Unit\Models;

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

class RangeDoiTest extends TestCase
{
    use RefreshDatabase;

    protected $testUser;

    protected function setUp(): void
    {
        parent::setUp();
        
        $this->testUser = User::create([
            'name' => 'Test User',
            'email' => 'test@example.com',
            'password' => bcrypt('password'),
        ]);
    }

    /** @test */
    public function it_can_create_range_doi()
    {
        $rangeDoi = RangeDoi::create([
            'name' => 'Critical',
            'min_days' => 0,
            'max_days' => 3,
            'color_code' => '#dc3545',
            'description' => 'Critical stock level',
            'is_active' => true,
            'created_by' => $this->testUser->id,
            'delete_flag' => false
        ]);

        $this->assertDatabaseHas('m_range_dois', [
            'name' => 'Critical',
            'min_days' => 0,
            'max_days' => 3,
            'color_code' => '#dc3545'
        ]);
    }

    /** @test */
    public function it_casts_attributes_correctly()
    {
        $rangeDoi = RangeDoi::create([
            'name' => 'Normal',
            'min_days' => '3.5',
            'max_days' => '7.0',
            'color_code' => '#28a745',
            'is_active' => '1',
            'delete_flag' => '0',
            'created_by' => $this->testUser->id
        ]);

        $this->assertIsFloat($rangeDoi->min_days);
        $this->assertEquals(3.5, $rangeDoi->min_days);
        $this->assertIsFloat($rangeDoi->max_days);
        $this->assertEquals(7.0, $rangeDoi->max_days);
        $this->assertIsBool($rangeDoi->is_active);
        $this->assertTrue($rangeDoi->is_active);
        $this->assertIsBool($rangeDoi->delete_flag);
        $this->assertFalse($rangeDoi->delete_flag);
    }

    /** @test */
    public function it_has_correct_relationships()
    {
        $rangeDoi = RangeDoi::create([
            'name' => 'Test Range',
            'min_days' => 0,
            'max_days' => 5,
            'color_code' => '#007bff',
            'is_active' => true,
            'created_by' => $this->testUser->id,
            'updated_by' => $this->testUser->id,
            'delete_flag' => false
        ]);

        $this->assertInstanceOf(User::class, $rangeDoi->creator);
        $this->assertEquals($this->testUser->id, $rangeDoi->creator->id);

        $this->assertInstanceOf(User::class, $rangeDoi->modificator);
        $this->assertEquals($this->testUser->id, $rangeDoi->modificator->id);
    }

    /** @test */
    public function it_can_scope_active_ranges()
    {
        RangeDoi::create([
            'name' => 'Active Range',
            'min_days' => 0,
            'max_days' => 3,
            'color_code' => '#dc3545',
            'is_active' => true,
            'delete_flag' => false
        ]);

        RangeDoi::create([
            'name' => 'Inactive Range',
            'min_days' => 3,
            'max_days' => 7,
            'color_code' => '#28a745',
            'is_active' => false,
            'delete_flag' => false
        ]);

        RangeDoi::create([
            'name' => 'Deleted Range',
            'min_days' => 7,
            'max_days' => 14,
            'color_code' => '#ffc107',
            'is_active' => true,
            'delete_flag' => true
        ]);

        $activeRanges = RangeDoi::active()->get();
        $this->assertCount(1, $activeRanges);
        $this->assertEquals('Active Range', $activeRanges->first()->name);
    }

    /** @test */
    public function it_can_check_if_value_is_in_range()
    {
        $criticalRange = RangeDoi::create([
            'name' => 'Critical',
            'min_days' => 0,
            'max_days' => 3,
            'color_code' => '#dc3545',
            'is_active' => true,
            'delete_flag' => false
        ]);

        $normalRange = RangeDoi::create([
            'name' => 'Normal',
            'min_days' => 3.1,
            'max_days' => 7,
            'color_code' => '#28a745',
            'is_active' => true,
            'delete_flag' => false
        ]);

        // Test critical range
        $this->assertTrue($criticalRange->isInRange(2));
        $this->assertTrue($criticalRange->isInRange(3));
        $this->assertFalse($criticalRange->isInRange(4));

        // Test normal range
        $this->assertFalse($normalRange->isInRange(3));
        $this->assertTrue($normalRange->isInRange(5));
        $this->assertTrue($normalRange->isInRange(7));
        $this->assertFalse($normalRange->isInRange(8));
    }

    /** @test */
    public function it_displays_range_correctly()
    {
        $normalRange = RangeDoi::create([
            'name' => 'Normal',
            'min_days' => 3,
            'max_days' => 7,
            'color_code' => '#28a745',
            'is_active' => true,
            'delete_flag' => false
        ]);

        $overstockRange = RangeDoi::create([
            'name' => 'Overstock',
            'min_days' => 14,
            'max_days' => 999999,
            'color_code' => '#6f42c1',
            'is_active' => true,
            'delete_flag' => false
        ]);

        $this->assertEquals('3 - 7 days', $normalRange->range_display);
        $this->assertEquals('14+ days', $overstockRange->range_display);
    }

    /** @test */
    public function it_generates_color_badge_html()
    {
        $rangeDoi = RangeDoi::create([
            'name' => 'Critical',
            'min_days' => 0,
            'max_days' => 3,
            'color_code' => '#dc3545',
            'is_active' => true,
            'delete_flag' => false
        ]);

        $expectedBadge = '<span class="badge" style="background-color: #dc3545; color: white;">Critical</span>';
        $this->assertEquals($expectedBadge, $rangeDoi->color_badge);
    }

    /** @test */
    public function it_handles_null_color_code_in_badge()
    {
        $rangeDoi = RangeDoi::create([
            'name' => 'Test Range',
            'min_days' => 0,
            'max_days' => 3,
            'color_code' => null,
            'is_active' => true,
            'delete_flag' => false
        ]);

        $expectedBadge = '<span class="badge" style="background-color: #6c757d; color: white;">Test Range</span>';
        $this->assertEquals($expectedBadge, $rangeDoi->color_badge);
    }

    /** @test */
    public function it_can_get_doi_category_for_value()
    {
        RangeDoi::create([
            'name' => 'Critical',
            'min_days' => 0,
            'max_days' => 3,
            'color_code' => '#dc3545',
            'is_active' => true,
            'delete_flag' => false
        ]);

        RangeDoi::create([
            'name' => 'Normal',
            'min_days' => 3.1,
            'max_days' => 7,
            'color_code' => '#28a745',
            'is_active' => true,
            'delete_flag' => false
        ]);

        $this->assertEquals('Critical', RangeDoi::getDoiCategory(2));
        $this->assertEquals('Normal', RangeDoi::getDoiCategory(5));
        $this->assertEquals('Unknown', RangeDoi::getDoiCategory(10));
    }

    /** @test */
    public function it_can_get_doi_color_for_value()
    {
        RangeDoi::create([
            'name' => 'Critical',
            'min_days' => 0,
            'max_days' => 3,
            'color_code' => '#dc3545',
            'is_active' => true,
            'delete_flag' => false
        ]);

        RangeDoi::create([
            'name' => 'Normal',
            'min_days' => 3.1,
            'max_days' => 7,
            'color_code' => '#28a745',
            'is_active' => true,
            'delete_flag' => false
        ]);

        $this->assertEquals('#dc3545', RangeDoi::getDoiColor(2));
        $this->assertEquals('#28a745', RangeDoi::getDoiColor(5));
        $this->assertEquals('#6c757d', RangeDoi::getDoiColor(10));
    }

    /** @test */
    public function it_ignores_inactive_ranges_in_static_methods()
    {
        RangeDoi::create([
            'name' => 'Inactive Critical',
            'min_days' => 0,
            'max_days' => 3,
            'color_code' => '#dc3545',
            'is_active' => false,
            'delete_flag' => false
        ]);

        RangeDoi::create([
            'name' => 'Active Normal',
            'min_days' => 0,
            'max_days' => 7,
            'color_code' => '#28a745',
            'is_active' => true,
            'delete_flag' => false
        ]);

        $this->assertEquals('Active Normal', RangeDoi::getDoiCategory(2));
        $this->assertEquals('#28a745', RangeDoi::getDoiColor(2));
    }

    /** @test */
    public function it_uses_correct_table_name()
    {
        $rangeDoi = new RangeDoi();
        $this->assertEquals('m_range_dois', $rangeDoi->getTable());
    }
}