<?php

namespace App\Models;

use CodeIgniter\Model;

class PaymentTransactionModel extends Model
{
    protected $table = 'payment_transactions'; // Table name
    protected $primaryKey = 'id';  // Primary key

    // Allowed fields for mass assignment
    protected $allowedFields = [
        'transaction_id',
        'payment_id',               // Reference to payments table (the associated payment)
        'transaction_date',
        'amount',
        'payment_method',
        'payment_status',
        'transaction_fee',
        'semester',
        'school_year'
    ];

    protected $useTimestamps = false;

    // Validation rules
    protected $validationRules = [
        'transaction_id' => 'required|is_unique[payment_transactions.transaction_id]',
        'payment_id' => 'required|integer',  // Reference to payment table
        'amount' => 'required|decimal',
        'payment_method' => 'required|string|max_length[50]',
        'payment_status' => 'required|string|max_length[50]',
        'semester' => 'permit_empty|string|max_length[30]',
        'school_year' => 'permit_empty|string|max_length[9]',
    ];

    // Custom error messages
    protected $validationMessages = [
        'transaction_id' => [
            'required' => 'The transaction ID is required.',
            'is_unique' => 'The transaction ID must be unique.',
        ],
        'payment_id' => [
            'required' => 'The payment ID is required.',
            'integer' => 'The payment ID must be an integer.',
        ],
        'amount' => [
            'required' => 'The payment amount is required.',
            'decimal' => 'The payment amount must be a valid decimal number.',
        ],
        'payment_method' => [
            'required' => 'The payment method is required.',
            'max_length' => 'The payment method must not exceed 50 characters.',
        ],
        'payment_status' => [
            'required' => 'The payment status is required.',
            'max_length' => 'The payment status must not exceed 50 characters.',
        ],
    ];

    /**
     * Get all transactions for a specific payment.
     *
     * @param int $paymentId
     * @return array
     */
    public function getTransactionsByPaymentId($paymentId)
    {
        return $this->where('payment_id', $paymentId)
            ->orderBy('transaction_date', 'DESC')
            ->findAll();
    }

    /**
     * Retrieve a single transaction by its transaction ID.
     *
     * @param string $transactionId
     * @return array|null
     */
    public function getTransactionById($transactionId)
    {
        return $this->where('transaction_id', $transactionId)->first();
    }

    /**
     * Update payment status by transaction ID.
     *
     * @param string $transactionId
     * @param string $status
     * @return bool
     */
    public function updateTransactionStatus($transactionId, $status)
    {
        return $this->where('transaction_id', $transactionId)
            ->set(['payment_status' => $status])
            ->update();
    }

    /**
     * Get total amount paid for a specific payment.
     *
     * @param int $paymentId
     * @return float
     */
    public function getTotalPaidByPaymentId($paymentId)
    {
        $this->selectSum('amount');
        return $this->where('payment_id', $paymentId)->get()->getRow()->amount;
    }

    /**
     * Update transaction fee for a specific transaction.
     *
     * @param string $transactionId
     * @param float $transactionFee
     * @return bool
     */
    public function updateTransactionFee($transactionId, $transactionFee)
    {
        return $this->where('transaction_id', $transactionId)
            ->set(['transaction_fee' => $transactionFee])
            ->update();
    }

    /**
     * Get the latest payment transaction for a payment.
     *
     * @param int $paymentId
     * @return array|null
     */
    public function getLatestTransactionByPaymentId($paymentId)
    {
        return $this->where('payment_id', $paymentId)
            ->orderBy('transaction_date', 'DESC')
            ->first();
    }
}
