<?php

namespace Tests\Feature\Bookings;

use App\Classes\ValueObjects\Constants\BankAccountType;
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 CreateBookingControllerTest 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
        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'
        ]));

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

        // create recipient bank
        DB::table('banks')->insert([
            'company_id' => $company->id,
            'bank_name' => 'Maybank',
            'holder_name' => 'user one recipient',
            'account_no' => '123456789',
            'country_id' => 2,
            'status' => 2,
            'type' => 2,
        ]);

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

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

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

        $foreign_total = 1000;

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

        $recipient_bank = $company->banks->where('type', BankAccountType::EXTERNAL)->first();

        $response = $this->postJson(route('api.booking.create', ['company_id' => $company->id]), 
        [
            'convertible_currency_id' => 2,
            'fix_amount' => json_decode($quotation_response->getContent(), true)['payload']['data']['foreign_total'],
            'service_id' => 1,
            'transferable_bank_id' => $recipient_bank->id,
            'type' => 1,
        ]);

        $response->assertStatus(200);

        $this->assertDatabaseHas("bookings", [
            "company_id" => $company->id,
            "service_id" => 1,
            "bank_id" => $recipient_bank->id,
            "fix_amount" => $foreign_total,
            "fix_currency_id" => 2,
            "convertible_currency_id" => 2,
            "conversion_currency_id" => 1,
            "status" => 2
        ]);
    }
}
