<?php

namespace Tests\Feature;

use App\Models\AdminUser;
use App\Models\JobSeekerSubscriptionRates; 
use App\Modules\Admin\Subscriptions\JobSeekerSubscriptionRateModule; 

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;

use Laravel\Sanctum\Sanctum;

use Tests\TestCase;

class SystemSubscriptionRateJobSeekerTest extends TestCase
{
    use RefreshDatabase;

    private JobSeekerSubscriptionRates $jobSeekerSubscriptionRate; 
    
    /**
     * Set Up Test
     * 
     * @return void 
     */
    protected function setUp(): void
    {
        parent::setUp();

        $superAdmin = AdminUser::factory()->create();

        Sanctum::actingAs(
            $superAdmin,
            ['*']
        );

        $this->jobSeekerSubscriptionRate = JobSeekerSubscriptionRates::factory()
            ->state(
                [
                    'name' => "Beginner"
                ]
            )
            ->create();
    }

    /**
     * Test List Items
     * 
     * @return void 
     */
    public function test_list(): void
    {
        JobSeekerSubscriptionRates::factory()->count(20)->create();

        $response = (new JobSeekerSubscriptionRateModule())->getData([]);

        $this->assertTrue($response->status() == 200, "HTTP Status Code is 200");

        $data = $response->getData(true);

        $this->assertIsArray($data);
        $this->assertArrayHasKey('data', $data);
        $this->assertArrayHasKey('links', $data);
        $this->assertArrayHasKey('meta', $data);

        $this->assertTrue(count($data['data']) >= 10);
    }

    /**
     * Test View Item 
     * 
     * @return void 
     */
    public function test_view(): void
    {
        $response = (new JobSeekerSubscriptionRateModule())->getDetails($this->jobSeekerSubscriptionRate->id);

        $this->assertTrue($response->status() == 200, "HTTP Status Code is 200");

        $data = $response->getData(true);

        $this->assertTrue(count($data['data']) >= 1);

        $this->assertEquals('Beginner', $data['data'][0]['name'], "Retrieved Item's name must be 'Beginner'");
    }

    /**
     * Test Create Item
     * 
     * @return void 
     */
    public function test_create(): void
    {
        $response = (new JobSeekerSubscriptionRateModule())->storeData(
            [
                'name'        =>  'Enterprise',
                'description' =>  'For Dedicated Seeker',
                'month'       =>  12,
                'rate'        =>  40,
            ]
        );

        $this->assertTrue($response->status() == 201, "HTTP Status Code is 201");

        $this->assertDatabaseHas(
            JobSeekerSubscriptionRates::class,
            [
                'name'        =>  'Enterprise',
                'description' =>  'For Dedicated Seeker',
                'month'       =>  12,
                'rate'        =>  40,
            ]
        );
    }

    /**
     * Test Update Item
     * 
     * @return void 
     */
    public function test_update(): void
    {
        $response = (new JobSeekerSubscriptionRateModule())->updateData(
            [
                'name'        =>  'Beginner',
                'description' =>  'For Dedicated Seeker',
                'month'       =>  3,
                'rate'        =>  5,
            ],
            $this->jobSeekerSubscriptionRate->id
        );

        $this->assertTrue($response->status() == 200, "HTTP Status Code is 200");

        $this->assertDatabaseHas(
            JobSeekerSubscriptionRates::class,
            [
                'name'        =>  'Beginner',
                'description' =>  'For Dedicated Seeker',
                'month'       =>  3,
                'rate'        =>  5,
            ]
        );
    }

    /**
     * Test Enable Item
     * 
     * @return void 
     */
    public function test_enable(): void
    {
        $response = (new JobSeekerSubscriptionRateModule())->enableDisableData(
            [
                'is_disabled' => false,
            ],
            $this->jobSeekerSubscriptionRate->id
        );

        $this->assertTrue($response->status() == 200, "HTTP Status Code is 200");

        $this->assertDatabaseHas(
            JobSeekerSubscriptionRates::class,
            [
                'id'          => $this->jobSeekerSubscriptionRate->id,
                'is_disabled' => false,
            ]
        );
    }

    /**
     * Test Disable Item
     * 
     * @return void 
     */
    public function test_disable(): void
    {
        $response = (new JobSeekerSubscriptionRateModule())->enableDisableData(
            [
                'is_disabled' => true,
            ],
            $this->jobSeekerSubscriptionRate->id
        );

        $this->assertTrue($response->status() == 200, "HTTP Status Code is 200");

        $this->assertDatabaseHas(
            JobSeekerSubscriptionRates::class,
            [
                'id'          => $this->jobSeekerSubscriptionRate->id,
                'is_disabled' => true,
            ]
        );
    }
}
