<?php

namespace App\Models;

class QuoteModel extends BaseCompanyModel
{
    protected $table = 'quotes';
    protected $primaryKey = 'id';
    protected $useTimestamps = true;
    protected $allowedFields = [
        'company_id', 'client_id', 'created_by', 'quote_number', 'amount', 'currency',
        'issue_date', 'valid_until', 'status', 'converted_invoice_id', 'notes',
    ];

    public function generateNumber(?int $companyId): string
    {
        $companyId = $companyId ?? 0;
        $year = date('Y');
        $count = $this->where('company_id', $companyId)
            ->where('YEAR(created_at)', $year)
            ->countAllResults() + 1;

        return sprintf('QUO-%d-%s-%04d', $companyId, $year, $count);
    }
}
