<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class ReminderSetting extends Model
{
    protected $fillable = [
        'company_id',
        'enable_invoice_created',
        'enable_invoice_due_reminder',
        'enable_invoice_overdue_reminder',
        'enable_invoice_paid',
        'enable_partial_payment',
        'enable_quotation_created',
        'enable_quotation_expiry',
        'enable_quotation_accepted',
        'enable_quotation_rejected',
        'enable_purchase_order_created',
        'enable_recurring_invoice_generated',
        'enable_payment_receipt',
        'enable_manual_reminders',
        'enable_customer_welcome',
        'enable_follow_up_reminder',
        'due_reminder_days_before',
        'overdue_reminder_days_after',
        'quotation_expiry_days_before',
        'send_copy_to_admin',
        'email_sending_mode',
        'attach_pdf_to_emails',
    ];

    protected function casts(): array
    {
        return [
            'enable_invoice_created' => 'boolean',
            'enable_invoice_due_reminder' => 'boolean',
            'enable_invoice_overdue_reminder' => 'boolean',
            'enable_invoice_paid' => 'boolean',
            'enable_partial_payment' => 'boolean',
            'enable_quotation_created' => 'boolean',
            'enable_quotation_expiry' => 'boolean',
            'enable_quotation_accepted' => 'boolean',
            'enable_quotation_rejected' => 'boolean',
            'enable_purchase_order_created' => 'boolean',
            'enable_recurring_invoice_generated' => 'boolean',
            'enable_payment_receipt' => 'boolean',
            'enable_manual_reminders' => 'boolean',
            'enable_customer_welcome' => 'boolean',
            'enable_follow_up_reminder' => 'boolean',
            'send_copy_to_admin' => 'boolean',
            'attach_pdf_to_emails' => 'boolean',
        ];
    }

    public static function current(): self
    {
        return static::query()->orderBy('id')->first()
            ?? static::create([
                'email_sending_mode' => 'queue',
            ]);
    }

    public function usesQueue(): bool
    {
        return $this->email_sending_mode === 'queue';
    }
}
