<?php

namespace App\Console\Commands;

use App\Models\GeneratedReport;
use App\Services\Mail\ReportMailer;
use Illuminate\Console\Command;

class SendReportCommand extends Command
{
    protected $signature = 'dw:send-report {report_id} {--to=*} {--cc=*} {--bcc=*} {--subject=} {--note=} {--pdf} {--excel} {--csv} {--html}';

    protected $description = 'Oluşturulmuş bir raporu SMTP ile mail olarak gönderir.';

    public function handle(ReportMailer $mailer): int
    {
        $report = GeneratedReport::findOrFail((int) $this->argument('report_id'));

        $emailLog = $mailer->sendReport($report, [
            'recipients' => $this->option('to'),
            'cc' => $this->option('cc'),
            'bcc' => $this->option('bcc'),
            'subject' => $this->option('subject'),
            'note' => $this->option('note'),
            'include_pdf' => $this->option('pdf') || (! $this->option('excel') && ! $this->option('csv') && ! $this->option('html')),
            'include_excel' => $this->option('excel') || (! $this->option('pdf') && ! $this->option('csv') && ! $this->option('html')),
            'include_csv' => $this->option('csv'),
            'include_html' => $this->option('html'),
        ]);

        $this->info('Mail log #' . $emailLog->id . ' / ' . $emailLog->status);

        if ($emailLog->error_message) {
            $this->error($emailLog->error_message);
        }

        return $emailLog->status === 'sent' ? self::SUCCESS : self::FAILURE;
    }
}
