<?php

declare(strict_types=1);

namespace common\models;

use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;

/**
 * Payment ActiveRecord model. Supports partial payments against invoices.
 *
 * @property int         $id
 * @property int         $invoice_id
 * @property int         $customer_id
 * @property float       $amount
 * @property string      $payment_method  cash|upi|card|bank_transfer|cheque|other
 * @property string      $payment_date
 * @property string|null $reference_no
 * @property string|null $notes
 * @property int|null    $created_by
 * @property int         $created_at
 *
 * @property Invoice  $invoice
 * @property Customer $customer
 */
class Payment extends ActiveRecord
{
    public const METHOD_CASH          = 'cash';
    public const METHOD_UPI           = 'upi';
    public const METHOD_CARD          = 'card';
    public const METHOD_BANK_TRANSFER = 'bank_transfer';
    public const METHOD_CHEQUE        = 'cheque';
    public const METHOD_OTHER         = 'other';

    public static function tableName(): string
    {
        return '{{%payments}}';
    }

    public function behaviors(): array
    {
        return [
            ['class' => TimestampBehavior::class, 'updatedAtAttribute' => false],
        ];
    }

    public function rules(): array
    {
        return [
            [['invoice_id', 'customer_id', 'amount', 'payment_date'], 'required'],
            [['invoice_id', 'customer_id', 'created_by'], 'integer'],
            [['amount'], 'number', 'min' => 0.01],
            [['payment_date'], 'date', 'format' => 'php:Y-m-d'],
            [['payment_method'], 'in', 'range' => [self::METHOD_CASH, self::METHOD_UPI, self::METHOD_CARD, self::METHOD_BANK_TRANSFER, self::METHOD_CHEQUE, self::METHOD_OTHER]],
            [['payment_method'], 'default', 'value' => self::METHOD_CASH],
            [['reference_no'], 'string', 'max' => 100],
            [['notes'], 'string'],
        ];
    }

    public function attributeLabels(): array
    {
        return [
            'id'             => 'ID',
            'invoice_id'     => 'Invoice',
            'customer_id'    => 'Customer',
            'amount'         => 'Amount (₹)',
            'payment_method' => 'Payment Method',
            'payment_date'   => 'Payment Date',
            'reference_no'   => 'Reference No.',
            'notes'          => 'Notes',
            'created_at'     => 'Created At',
        ];
    }

    public function getInvoice(): \yii\db\ActiveQuery
    {
        return $this->hasOne(Invoice::class, ['id' => 'invoice_id']);
    }

    public function getCustomer(): \yii\db\ActiveQuery
    {
        return $this->hasOne(Customer::class, ['id' => 'customer_id']);
    }

    public static function methodLabels(): array
    {
        return [
            self::METHOD_CASH          => 'Cash',
            self::METHOD_UPI           => 'UPI',
            self::METHOD_CARD          => 'Card',
            self::METHOD_BANK_TRANSFER => 'Bank Transfer',
            self::METHOD_CHEQUE        => 'Cheque',
            self::METHOD_OTHER         => 'Other',
        ];
    }
}
