<?php
ini_set('memory_limit', '-1');

use App\Classes\Jobs\GenerateGroupTransactionsWhiteForm;
use App\Classes\Modules\Documents\Services\CreatesDocument;
use App\Classes\Modules\Documents\Services\CreatesFiles;
use App\Classes\Modules\Transactions\Services\GeneratesGroupTransactionBillNumber;
use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Models\Transaction;
use App\Models\Group;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class RecoverGroupTransactionTableSeeder extends Seeder
{

    /** @var CreatesDocument */
    private $createsDocument;

    /** @var CreatesFiles */
    private $createsFile;

    /** @var GeneratesGroupTransactionBillNumber */
    private $generatesGroupTransactionBillNumber;

    /**
     * RecoverGroupTransactionTableSeeder constructor.
     * @param CreatesDocument $createsDocument
     * @param CreatesFiles $createsFile
     * @param GeneratesGroupTransactionBillNumber $generatesGroupTransactionBillNumber
     */
    public function __construct(CreatesDocument $createsDocument, CreatesFiles $createsFile, GeneratesGroupTransactionBillNumber $generatesGroupTransactionBillNumber)
    {
        $this->createsDocument = $createsDocument;
        $this->createsFile = $createsFile;
        $this->generatesGroupTransactionBillNumber = $generatesGroupTransactionBillNumber;
    }


    /**
     * Run the database seeds.
     *
     */
    public function run()
    {
        DB::beginTransaction();

        $transaction_group = Transaction::
            select('issuer', 'currency_rate', 'type', DB::raw('count(DISTINCT id) as total'), DB::raw("DATE_FORMAT(created_at, '%Y-%m-%d %H:%i') as new_date"))
            ->where('type', 3)
            ->groupBy(
                'issuer',
                'currency_rate',
                'new_date'
            )
            ->orderBy('id')->get();

        foreach ($transaction_group as $group) {
            $transactions = Transaction::
                where('type', 3)
                ->where('issuer', $group->issuer)
                ->where('currency_rate', $group->currency_rate)
                ->where(DB::raw("DATE_FORMAT(created_at, '%Y-%m-%d %H:%i')"), $group->new_date)
                ->get();

            $group = new Group();
            $group->save();

            $issuer = '';
            $date = now();
            $receiver = '';
            $amount = 0;
            $original_amount = 0;
            $currency_id = 0;
            $original_currency_id = '';
            $currency_rate = '';
            $tax = 0;
            $service_charge = 0;

            foreach ($transactions as $transaction) {
                $group->transactions()->sync($transaction->id, false);
                $issuer = $transaction->issuer;
                $date = $transaction->created_at;
                $receiver = $transaction->receiver;
                $amount += $transaction->amount;
                $original_amount += $transaction->original_amount;
                $currency_id = $transaction->currency_id;
                $original_currency_id = $transaction->original_currency_id;
                $currency_rate = $transaction->currency_rate;
                $tax += $transaction->tax;
                $service_charge += $transaction->service_charge;
            }

            $group->issuer = $issuer;
            $group->receiver = $receiver;
            $group->reference = $this->generatesGroupTransactionBillNumber->execute('SPO-', $date);
            $group->amount = $amount;
            $group->original_amount = $original_amount;
            $group->currency_id = $currency_id;
            $group->original_currency_id = $original_currency_id;
            $group->currency_rate = $currency_rate;
            $group->tax = $tax;
            $group->service_charge = $service_charge;
            $group->status = ApprovalStatus::PENDING_SUBMISSION;
            $group->created_at = $date;
            $group->updated_at = $date;

            $group->update();
        }

        DB::commit();

        GenerateGroupTransactionsWhiteForm::dispatch();
    }
}
