<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Console\Commands\AbstractCommand;
use App\Modules\Invoices\InvoiceTransaction;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log;
use App\Seraph\Services\GlobalMailerService;

class EmailCashReport extends AbstractCommand
{
    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'email:cash-report';

    /**
     * The command title that will be displayed in the logs.
     *
     * @var string
     */
    protected $command_title = 'EmailCashReport';

    /**
     * The destination of the email.
     *
     * @var string
     */
    protected $sendTo = '';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Sends an email with the cash report for the specified date range.';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return void
     */
    public function fire()
    {
        // Mail::pretend();

        $startDate = Carbon::today()->toDateTimeString();
        $endDate = Carbon::tomorrow()->toDateTimeString();

        // Get each amount
        $cash = round(InvoiceTransaction::where('method_of_payment', 'cash')->where('created_at', '>=', $startDate)->where('created_at', '<', $endDate)->sum('amount'), 2);
        $card = round(InvoiceTransaction::where('method_of_payment', 'card')->where('created_at', '>=', $startDate)->where('created_at', '<', $endDate)->sum('amount'), 2);
        $cheque = round(InvoiceTransaction::where('method_of_payment', 'cheque')->where('created_at', '>=', $startDate)->where('created_at', '<', $endDate)->sum('amount'), 2);
        $bacs = round(InvoiceTransaction::where('method_of_payment', 'bacs')->where('created_at', '>=', $startDate)->where('created_at', '<', $endDate)->sum('amount'), 2);

        $this->info($cash.' in cash transactions');
        $this->info($card.' in card transactions');
        $this->info($cheque.' in cheque transactions');
        $this->info($bacs.' in bacs transactions');

        GlobalMailerService::send('emails.alerts.cash-report', compact('startDate', 'endDate', 'cash', 'card', 'cheque', 'bacs'), function ($message) {
            $message->to($this->sendTo)->subject('Daily cash report '.Carbon::today()->format('d/m/Y'))->priority(1);
        });

        $this->info('Cash report sent to '.$this->sendTo);
    }

    /**
     * Get the console command arguments.
     *
     * @return array
     */
    protected function getArguments()
    {
        return [

        ];
    }

    /**
     * Get the console command options.
     *
     * @return array
     */
    protected function getOptions()
    {
        return [

        ];
    }
}
