<?php

namespace Tests\Feature\Companies;

use App\Classes\ValueObjects\Constants\SegmentConstants;
use App\Models\Company;
use Database\Seeders\CompaniesTableSeeder;
use Database\Seeders\CountriesTableSeeder;
use Database\Seeders\CurrenciesTableSeeder;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;

class FetchCompanyBookingQuotationControllerTest extends TestCase
{
    use DatabaseMigrations;

    private $curr_rate_std_cash = 1.52800;
    private $curr_rate_std_cheque = 1.51800;
    private $curr_rate_std_wallet = 1.53400;
    private $curr_rate_std_online = 1.53100;
    private $curr_rate_plat_cash = 1.54100;
    private $curr_rate_plat_cheque = 1.53100;
    private $curr_rate_plat_wallet = 1.54700;
    private $curr_rate_plat_online = 1.54400;

    protected function setUp(): void
    {
        parent::setUp();
        $this->seed(CountriesTableSeeder::class);
        $this->seed(CurrenciesTableSeeder::class);
        $this->seed(CompaniesTableSeeder::class);

        // create currency rate
        DB::table('currency_rates')->insert(array (
            0 => 
            array (
                'id' => 1,
                'currency_id' => 2,
                'selling' => $this->curr_rate_std_cash,
                'payment_method_type' => 1,
                'service_id' => 1,
            ),
            1 => 
            array (
                'id' => 2,
                'currency_id' => 2,
                'selling' => $this->curr_rate_std_cheque,
                'payment_method_type' => 2,
                'service_id' => 1,
            ),
            2 => 
            array (
                'id' => 3,
                'currency_id' => 2,
                'selling' => $this->curr_rate_std_wallet,
                'payment_method_type' => 4,
                'service_id' => 1,
            ),
            3 => 
            array (
                'id' => 4,
                'currency_id' => 2,
                'selling' => $this->curr_rate_std_online,
                'payment_method_type' => 5,
                'service_id' => 1,
            )
        ));

        // create service
        DB::table('service_types')->insert([
            'id' => 1,
            'name' => 'X1 Transfer',
            'status' => 2,
        ]);

        // create standard segment
        DB::table('segments')->insert([
            'id' => 1,
            'name' => 'Standard Segment',
            'type' => SegmentConstants::STANDARD_SEGMENT,
            'status' => 2
        ]);
        // create platinum segment
        DB::table('segments')->insert([
            'id' => 2,
            'name' => 'platinum member',
            'type' => SegmentConstants::CUSTOM_SEGMENT,
            'status' => 2,
        ]);
        // create standard segment constant
        DB::table('segment_constants')->insert([
            'id' => 1,
            'segment_id' => 1,
            'name' => 'Service Type',
            'reference' => 'SERVICE_TYPE',
            'detail' => '{"id":1,"bank_id":1,"service_charge":{"type":"percentage","value":2},"minimum_charge":{"type":"fixed_amount","value":10},"tax":{"type":"percentage","value":0},"po_limit":{"type":"fixed_amount","value":3},"is_active":true,"is_billable":true,"currencies":[{"id":2,"max_limit":{"type":"fixed_amount","value":500000},"min_limit":{"type":"fixed_amount","value":100},"rates":[]}]}',
        ]);
        // create platinum segment constant
        // Rate
        // 1. Cash: 
        DB::table('segment_constants')->insert([
            'id' => 2,
            'segment_id' => 2,
            'name' => 'Custom Service Type',
            'reference' => 'CUSTOM_SERVICE_TYPE',
            'detail' => '{"id":1,"minimum_charge":{"type":"fixed_amount","value":0},"is_active":true,"is_billable":true,"currencies":[{"id":2,"max_limit":{"type":"fixed_amount","value":500000},"min_limit":{"type":"fixed_amount","value":0.01},"rates":[{"payment_type":1,"payment_method":"cash","selling":{"type":"rate","value":"1.54100"}},{"payment_type":2,"payment_method":"cheque","selling":{"type":"rate","value":"1.53100"}},{"payment_type":4,"payment_method":"wallet","selling":{"type":"rate","value":"1.54700"}},{"payment_type":5,"payment_method":"payment_gateway","selling":{"type":"rate","value":"1.54400"}}]}]}',
        ]);

        // create a user
        $this->postJson(route('api.account.registration.register', [
            "company_name" => "",
            "name" => "user one",
            "email" => "user1@mail.com",
            "password" => "password",
            "password_confirmation" => "password",
            "phone" => "0123456789",
            "type" => 0
        ]));

        $response = $this->postJson(route('api.account.authentication.authenticate.attempt', [
            'email' => 'user1@mail.com',
            'password' => 'password'
        ]));

        $token = json_decode($response->getContent(), true)['payload']['access_token'];

        $this->withHeaders([
            'Accept' => 'application/json',
            'Authorization' => "Bearer $token",
            'Captcha-Token' => "test"
        ]);
        
    }

    public function test_standard_customer_fetch_booking_quotation_cash()
    {
        $company = Company::where('name', 'user one')->first();

        $foreign_total = 1000;
        $local_total = 1000 / $this->curr_rate_std_cash;
        $conversion_amount = $local_total;
        $service_charge = $conversion_amount * 0.02;
        $sub_total = $local_total + $service_charge;


        $response = $this->postJson(route('api.company.currency.convert', [
            'id' => $company->id,
            'amount' => $foreign_total,
            'currency_id' => 2,
            'service_id' => 1,
            'type' => 1,
        ]));

        $response->assertStatus(200)->assertJsonStructure([
            "title",
            "message",
            "payload" => [
                "data" => [
                    "conversion_amount",
                    "foreign_total",
                    "local_total",
                    "rate",
                    "service_charge",
                    "sub_total",
                    "tax",
                    "tax_total",
                    "total",
                    "voucher_code",
                    "voucher_discount_amount"
                ]
            ]
        ]);

        $data = json_decode($response->getContent(), true)['payload']['data'];
        $this->assertEqualsWithDelta($conversion_amount, $data['conversion_amount'], 0.0000001);
        $this->assertEqualsWithDelta($foreign_total, $data['foreign_total'], 0.0000001);
        $this->assertEqualsWithDelta($local_total, $data['local_total'], 0.0000001);
        $this->assertEqualsWithDelta($this->curr_rate_std_cash, $data['rate'], 0.0000001);
        $this->assertEqualsWithDelta($service_charge, $data['service_charge'], 0.0000001);
        $this->assertEqualsWithDelta($sub_total, $data['sub_total'], 0.0000001);
        $this->assertEquals(0, $data['tax']);
        $this->assertEquals(0, $data['tax_total']);
        $this->assertEqualsWithDelta($sub_total, $data['total'], 0.0000001);
        $this->assertEquals("", $data['voucher_code']);
        $this->assertEquals(0, $data['voucher_discount_amount']);

    }
}
