<?php

namespace Tests\Feature;

use App\Models\AdminUser;
use App\Models\CompanySubscriptionRates;
use App\Modules\Admin\Subscriptions\CompanySubscriptionRateModule;

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

use Laravel\Sanctum\Sanctum;

use Tests\TestCase;

class SystemSubscriptionRateCompanyTest extends TestCase
{
    use RefreshDatabase;

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

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

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

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

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

        $response = (new CompanySubscriptionRateModule())->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 CompanySubscriptionRateModule())->getDetails($this->companySubscriptionRate->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 CompanySubscriptionRateModule())->storeData(
            [
                'name'         => "Enterprise",
                'description'  => "For Large Companies",
                'monthly_rate' => 50,
                'yearly_rate'  => 500,
                'max_employee' => 1000
            ]
        );

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

        $this->assertDatabaseHas(
            CompanySubscriptionRates::class,
            [
                'name'         => "Enterprise",
                'description'  => "For Large Companies",
                'monthly_rate' => 50,
                'yearly_rate'  => 500,
                'max_employee' => 1000
            ]
        );
    }

    /**
     * Test Update Item
     * 
     * @return void 
     */
    public function test_update(): void
    {
        $response = (new CompanySubscriptionRateModule())->updateData(
            [
                'name'         => "Start-ups",
                'description'  => "For Start-ups",
                'monthly_rate' => 20,
                'yearly_rate'  => 200,
                'max_employee' => 20
            ],
            $this->companySubscriptionRate->id
        );

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

        $this->assertDatabaseHas(
            CompanySubscriptionRates::class,
            [
                'name'         => "Start-ups",
                'description'  => "For Start-ups",
                'monthly_rate' => 20,
                'yearly_rate'  => 200,
                'max_employee' => 20
            ]
        );
    }

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

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

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

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

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

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