<?php

namespace PortalPeople\invoicing\traits;

use backend\models\Cartsbe;

/**
 * Invoice Rules Trait
 *
 * Provides reusable validation rules for invoice models.
 * Composes cart rules with invoice-specific validation.
 */
trait InvoiceRulesTrait
{
    /**
     * Get base cart rules for composition
     *
     * @return array
     */
    protected function getCartRules(): array
    {
        return (new Cartsbe())->rules();
    }

    /**
     * Get invoice-specific rules
     *
     * @return array
     */
    protected function getInvoiceSpecificRules(): array
    {
        return [
            [['invoice_number'], 'required', 'message' => 'Invoice number is required'],
            [['source_cart_id', 'source_centrearrddate'], 'required'],
            [['source_cart_id'], 'integer'],
            [['source_centrearrddate'], 'datetime', 'format' => 'php:Y-m-d'],
            [['generated_at'], 'datetime'],
            [['generation_key'], 'string', 'max' => 255],
            [['source_snapshot_json'], 'string'],
        ];
    }

    /**
     * Get composed rules (cart + invoice-specific)
     *
     * @return array
     */
    public function getComposedRules(): array
    {
        $rules = $this->getCartRules();
        return array_merge($rules, $this->getInvoiceSpecificRules());
    }
}
