<?php

namespace App\Controllers\View;

use App\Models\PaymentTransactionModel;
use CodeIgniter\RESTful\ResourceController;

class PaymentTransactionController extends ResourceController
{
    protected $paymentTransactionModel;

    public function __construct()
    {
        $this->paymentTransactionModel = new PaymentTransactionModel();
    }

    // API: Create a new payment transaction (installment)
    public function createAPI()
    {
        $data = [
            'transaction_id' => $this->request->getPost('transaction_id'),
            'payment_id' => $this->request->getPost('payment_id'),
            'transaction_date' => $this->request->getPost('transaction_date'),
            'amount' => $this->request->getPost('amount'),
            'payment_method' => $this->request->getPost('payment_method'),
            'payment_status' => 'Pending',
            'transaction_fee' => $this->request->getPost('transaction_fee'),
            'payment_reference' => $this->request->getPost('payment_reference'),
            'is_full_payment' => $this->request->getPost('is_full_payment') ? 1 : 0,
        ];

        if ($this->paymentTransactionModel->save($data)) {
            return $this->respondCreated($data);
        } else {
            return $this->failValidationErrors($this->paymentTransactionModel->errors());
        }
    }

    // API: Get all transactions for a specific payment
    public function getByPaymentAPI($paymentId)
    {
        $transactions = $this->paymentTransactionModel->getTransactionsByPaymentId($paymentId);
        if ($transactions) {
            return $this->respond($transactions);
        } else {
            return $this->failNotFound('Transactions not found for the payment.');
        }
    }

    // View: Get all transactions for a specific payment (for web views)
    public function getByPayment($paymentId)
    {
        $transactions = $this->paymentTransactionModel->getTransactionsByPaymentId($paymentId);
        return view('payment_transaction_list', ['transactions' => $transactions]);
    }

    // View: Create a new payment transaction
    public function create()
    {
        return view('payment_transaction_create');
    }

    // API: Update payment status by transaction ID
    public function updateStatusAPI($transactionId)
    {
        $status = $this->request->getPost('status');
        if ($this->paymentTransactionModel->updateTransactionStatus($transactionId, $status)) {
            return $this->respond(['status' => 'success']);
        } else {
            return $this->failNotFound('Transaction not found.');
        }
    }

    // View: Update payment status by transaction ID
    public function updateStatus($transactionId)
    {
        $status = $this->request->getPost('status');
        if ($this->paymentTransactionModel->updateTransactionStatus($transactionId, $status)) {
            return redirect()->to('/payment_transactions');
        } else {
            return redirect()->back()->with('error', 'Failed to update status.');
        }
    }
}