<?php

use Illuminate\Database\Seeder;
use App\Classes\ValueObjects\Constants as Constants;
use App\Models as Models;
use Illuminate\Support\Facades\DB;

class FakeBookingTransactions extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        if(App()->environment('local')) {
            //Add user to map company, and auth user for bookings
            $email = 'bookmaster@cief.com';
            $password = 'bo@k!ng$123';
            $booking_user = [
                'name' => 'Book Master',
                'email' => $email,
                'password' => bcrypt($password),
                'type' => Constants\RoleTypes::USER,
                'status' => Constants\ApprovalStatus::APPROVED,
            ];
            $user = Models\User::updateOrCreate(['email' => $email], $booking_user);
            $user_id = $user->id;

            //Add 3 Entity: 2 company + 1 individual
            $companies = [
                [
                    'name' => 'Maersk Line SDN BHD',
                    'reference' => 'MAERSK-123',
                    'type' => Constants\CompanyType::COMPANY_BUSINESS,
                    'business_type' => Constants\BusinessType::FREIGHT_FORWARDER,
                    'status' => Constants\ApprovalStatus::APPROVED,
                ],
                [
                    'name' => 'Sunrise Importer SDN BHD',
                    'reference' => 'SSMID-E',
                    'type' => Constants\CompanyType::COMPANY_BUSINESS,
                    'business_type' => Constants\BusinessType::IMPORTER,
                    'status' => Constants\ApprovalStatus::APPROVED,
                ],
                [
                    'name' => 'Han Solo',
                    'reference' => '880101-14-6541',
                    'type' => Constants\CompanyType::PERSONAL_BUSINESS,
                    'business_type' => Constants\BusinessType::IMPORTER,
                    'status' => Constants\ApprovalStatus::APPROVED,
                ]
            ];

            $addresses = [
                [
                    'company_id' => null, //fill-up later
                    'country_id' => 1,
                    'state_id' => 15,
                    'district_id' => 402,
                    'postcode' => '63000',
                    'street_one' => 'Vertex Tower, Cybersquare,',
                    'street_two' => 'Jalan Teknorat 6, Cyber 5',
                    'billing' => 1
                ]
            ];

            $contacts = [
                [
                    'company_id' => null, //fill-up later
                    'country_id' => 1,
                    'reference' => 'main',
                    'phone' => '0123456789',
                    'email' => 'email@edomain.com'
                ]
            ];

            $banks = [
                [
                    'company_id' => null, //fill-up later
                    'reference' => 'CBB',
                    'bank_name' => 'Citibank Berhad',
                    'holder_name' => '', //fill-up later, use company name
                    'account_no' => '0123456789',
                    'bank_branch' => 'Menara Citibank Kuala Lumpur',
                    'swift' => null,
                    'snap' => null,
                    'type' => Constants\BankAccountType::EXTERNAL,
                    'default' => 0,
                    'status' => 2,
                    'country_id' => 2,
                ]
            ];

            $company_ids = [];
            $address_ids = [];
            $contact_ids = [];
            $bank_ids = [];

            foreach ($companies as $company) {
                $check = ['name' => $company['name']];
                $added = Models\Company::updateOrCreate($check, $company);
                $company_ids[] = $added->id;

                //Add address
                foreach ($addresses as $address) {
                    $address['company_id'] = $added->id;
                    $address['street_one'] = $company['name'] . ", " . $address['street_one'];
                    $check_address = ['street_one' => $address['street_one']];
                    $added_address = Models\Address::updateOrCreate($check_address, $address);
                    $address_ids[$added->id][] = $added_address->id;
                }

                //Add contact
                foreach ($contacts as $contact) {
                    $contact['company_id'] = $added->id;
                    $contact['reference'] = $company['name'] . ", " . $contact['reference'];
                    $check_contact = ['reference' => $contact['reference']];
                    $added_contact = Models\Contact::updateOrCreate($check_contact, $contact);
                    $contact_ids[$added->id][] = $added_contact->id;
                }

                //Add bank
                foreach ($banks as $bank) {
                    $bank['company_id'] = $added->id;
                    $bank['reference'] = $company['name'] . ", " . $bank['reference'];
                    $check_bank = ['reference' => $bank['reference']];
                    $added_bank = Models\Bank::updateOrCreate($check_bank, $bank);
                    $bank_ids[$added->id][] = $added_bank->id;
                }

                //Model employee have issue: Unknown column 'updated_at' in 'field list'
                DB::table('employees')->updateOrInsert(
                    ['company_id' => $added->id, 'user_id' => $user_id, 'status' => 2],
                    ['company_id' => $added->id, 'user_id' => $user_id, 'status' => 2]
                );

                Models\Document::updateOrCreate(
                    ['owner_id' => $added->id, 'status' => 2],
                    [
                        'owner_type' => 'App\Models\Company',
                        'owner_id' => $added->id,
                        'status' => 2,
                        'document_type' => ($company['type'] == Constants\CompanyType::COMPANY_BUSINESS) ? Constants\DocumentType::SSM_REGISTRATION : Constants\DocumentType::IDENTITY_CARD,
                        'reference' => $company['reference'],
                    ]
                );

               Models\SegmentCompany::updateOrCreate(
                    ['company_id' => $added->id, 'segment_id' => 1],
                    ['company_id' => $added->id, 'segment_id' => 1]
                );
            }


            //Booking, Transactions require auth user
            $user = new Models\User;
            $user->email = $email;
            $user->password = $password;
            auth()->login($user);

            $bookings = [
                [
                    'company_id' => null, //fill-up later
                    'marking' => null, //fill-up later
                    'service_id' => 1,
                    'bank_id' => null, //fill-up later
                    'fix_amount' => null, //fill-up later
                    'fix_currency_id' => 2,
                    'convertible_currency_id' => 2,
                    'conversion_currency_id' => 1,
                    'status' => Constants\ApprovalStatus::APPROVED,
                ],
            ];

            $transactions = [
                [
                    'booking_id' => null, //fill-up later
                    'type' => null, //fill-up later
                    'issuer' => null, //fill-up later
                    'receiver' => 1,
                    'recipient_bank_account_id' => 1,
                    'payment_method' => 2,
                    'payment_reference' => 2,
                    'bill_no' => null, //fill-up later
                    'amount' => null, //fill-up later
                    'original_amount' => null, //fill-up later
                    'currency_id' => 2,
                    'original_currency_id' => 2,
                    'currency_rate' => 1.0,
                    'tax' => 1.0,
                    'service_charge' => 0.0,
                    'expires_on' => null,
                    'status' => 0,
                ],
            ];

            $transaction_details = [
                [
                    'transaction_id' => null, //fill-up later
                    'product_code' => 'MBP15',
                    'product_name' => 'Macbook Pro 15"',
                    'quantity' => 2,
                    'price' => 1000,
                    'amount' => 2000,
                ],
                [
                    'transaction_id' => null, //fill-up later
                    'product_code' => 'IP12',
                    'product_name' => 'iPad Pro 12"',
                    'quantity' => 2,
                    'price' => 1000,
                    'amount' => 2000,
                ],
                [
                    'transaction_id' => null, //fill-up later
                    'product_code' => 'IMC24',
                    'product_name' => 'iMac',
                    'quantity' => 2,
                    'price' => 1000,
                    'amount' => 2000,
                ],
            ];

            $booking_ids = [];
            $transaction_ids = [];
            $transaction_types = [
                Constants\TransactionType::PAYMENT_ATTEMPT,
                Constants\TransactionType::PAYMENT,
                Constants\TransactionType::INVOICE,
                Constants\TransactionType::BILL,
                Constants\TransactionType::PROFORMA,
                Constants\TransactionType::TOP_UP,
                Constants\TransactionType::REFUND,
                Constants\TransactionType::PURCHASE_ORDER,
                Constants\TransactionType::SUPPLIER_DELIVER,
            ];

            foreach ($company_ids as $company_id) {
                foreach ($transaction_types as $transaction_type) {
                    foreach ($bookings as $booking) {

                        $marking = mt_rand(20000, 20100); //Limit marking within 100 for easier to remember

                        $booking['company_id'] = $company_id;
                        $booking['bank_id'] = $bank_ids[$company_id][0];
                        $booking['marking'] = $marking;
                        $fix_amount = [4000,8000];
                        $booking['fix_amount'] = $fix_amount[array_rand($fix_amount)];;
                        $booking['created_at'] = date('Y-m-d H:i:s');
                        $booking['updated_at'] = date('Y-m-d H:i:s');

                        $check_booking = ['marking' => $booking['marking']];
                        $added_booking = Models\Booking::updateOrCreate($check_booking, $booking);
                        $booking_ids[] = $added_booking->id;
                        $booking_id = $added_booking->id;

                        foreach ($transactions as $transaction) {

                            $bill_no = $code = mt_rand(100000001, 999999999);

                            $transaction['booking_id'] = $booking_id;
                            $transaction['type'] = $transaction_type;
                            $transaction['issuer'] = $company_id;

                            $transaction['bill_no'] = $bill_no;
                            $transaction['amount'] = 6000;
                            $transaction['original_amount'] = 6000;

                            $transaction['created_at'] = date('Y-m-d H:i:s');
                            $transaction['updated_at'] = date('Y-m-d H:i:s');

                            $check_transaction = ['bill_no' => $transaction['bill_no']];
                            $added_transaction = Models\Transaction::updateOrCreate($check_transaction, $transaction);
                            $transaction_ids[] = $added_transaction->id;
                            $transaction_id = $added_transaction->id;

                            foreach ($transaction_details as $transaction_detail) {
                                $transaction_detail['transaction_id'] = $transaction_id;

                                $transaction_detail['created_at'] = date('Y-m-d H:i:s');
                                $transaction_detail['updated_at'] = date('Y-m-d H:i:s');

                                $check_transaction_details = [
                                    'transaction_id' => $transaction_detail['transaction_id'],
                                    'product_code' => $transaction_detail['product_code']
                                ];
                                Models\TransactionDetail::updateOrCreate($check_transaction_details,
                                    $transaction_detail);
                            }
                        }
                    }
                }
            }
        }
    }
}
