<?php

namespace App\Classes\Modules\Imports\Services;

use App\Models\AccountStatement;
use App\Models\StatementAccount;
use App\Models\StatementTransaction;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithHeadingRow;

class BankStatementImport implements ToCollection, WithHeadingRow
{
    public function collection(Collection $rows)
    {
        $accountNumber = null;

        foreach ($rows as $row) {
            if ($row->has('account_number')) {
                // If the row has an account number, create a new statement account
                $accountNumber = $row->get('account_number');
                $accountType = $row->get('account_type');
                $accountName = $row->get('account_name');
                $accountCurrency = $row->get('account_currency');

                $account = StatementAccount::updateOrCreate(
                    ['number' => $accountNumber],
                    [
                        'type' => $accountType,
                        'name' => $accountName,
                        'currency' => $accountCurrency,
                    ]
                );
            } else {
                // Otherwise, create a new statement transaction for the current statement account
                $dateFrom = $row->get('date_from');
                $dateTo = $row->get('date_to');
                $totalAmount = $row->get('total_amount');
                $beginBalance = $row->get('begin_balance');
                $endBalance = $row->get('end_balance');

                $statement = AccountStatement::updateOrCreate(
                    [
                        'account_id' => $account->id,
                        'date_from' => $dateFrom,
                        'date_to' => $dateTo,
                    ],
                    [
                        'total_amount' => $totalAmount,
                        'begin_balance' => $beginBalance,
                        'end_balance' => $endBalance,
                    ]
                );

                $transactionDate = $row->get('transaction_date');
                $transactionTime = $row->get('transaction_time');
                $postingDate = $row->get('posting_date');
                $transactionDescription = $row->get('transaction_description');
                $transactionRef = $row->get('transaction_ref');
                $amount = $row->get('amount');
                $tellerId = $row->get('teller_id');
                $branchChannel = $row->get('branch_channel');
                $transactionCode = $row->get('transaction_code');

                $transaction = new StatementTransaction([
                    'statement_id' => $statement->id,
                    'transaction_date' => $transactionDate,
                    'transaction_time' => $transactionTime,
                    'posting_date' => $postingDate,
                    'transaction_description' => $transactionDescription,
                    'transaction_ref' => $transactionRef,
                    'amount' => $amount,
                    'teller_id' => $tellerId,
                    'branch_channel' => $branchChannel,
                    'transaction_code' => $transactionCode,
                ]);

                $transaction->save();
            }
        }
    }
}
