<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Transactions extends Auth_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('Transaction_model');
        $this->load->model('Account_model');
    }

    /**
     * List all user transactions
     */
    public function index() {
        $page = $this->input->get('page') ?: 1;
        $limit = 20;
        $offset = ($page - 1) * $limit;
        
        $filters = [
            'type' => $this->input->get('type'),
            'status' => $this->input->get('status'),
            'date_from' => $this->input->get('from'),
            'date_to' => $this->input->get('to')
        ];
        
        $transactions = $this->Transaction_model->get_by_user($this->user->id, $filters, $limit, $offset);
        
        $this->data['title'] = 'Transactions';
        $this->data['transactions'] = $transactions;
        $this->data['filters'] = $filters;
        $this->data['page'] = $page;
        
        $this->render('transactions/index', $this->data);
    }

    /**
     * View transaction details
     */
    public function view($id) {
        $transaction = $this->Transaction_model->get($id);
        
        if (!$transaction) {
            show_404();
            return;
        }
        
        // Verify user owns the transaction
        $source_account = $transaction->source_account_id ? $this->Account_model->get($transaction->source_account_id) : null;
        $dest_account = $transaction->destination_account_id ? $this->Account_model->get($transaction->destination_account_id) : null;
        
        $is_owner = false;
        if ($source_account && $source_account->user_id == $this->user->id) {
            $is_owner = true;
        }
        if ($dest_account && $dest_account->user_id == $this->user->id) {
            $is_owner = true;
        }
        
        if (!$is_owner) {
            show_404();
            return;
        }
        
        // Determine if it's a debit or credit
        $is_debit = $source_account && $source_account->user_id == $this->user->id;
        
        $this->data['title'] = 'Transaction Details';
        $this->data['transaction'] = $transaction;
        $this->data['source_account'] = $source_account;
        $this->data['dest_account'] = $dest_account;
        $this->data['is_debit'] = $is_debit;
        
        $this->render('transactions/view', $this->data);
    }

    /**
     * Transaction receipt
     */
    public function receipt($id) {
        $transaction = $this->Transaction_model->get($id);
        
        if (!$transaction) {
            show_404();
            return;
        }
        
        // Similar verification as view
        $source_account = $transaction->source_account_id ? $this->Account_model->get($transaction->source_account_id) : null;
        $dest_account = $transaction->destination_account_id ? $this->Account_model->get($transaction->destination_account_id) : null;
        
        $is_owner = false;
        if ($source_account && $source_account->user_id == $this->user->id) $is_owner = true;
        if ($dest_account && $dest_account->user_id == $this->user->id) $is_owner = true;
        
        if (!$is_owner) {
            show_404();
            return;
        }
        
        $this->data['title'] = 'Transaction Receipt';
        $this->data['transaction'] = $transaction;
        $this->data['source_account'] = $source_account;
        $this->data['dest_account'] = $dest_account;
        
        $this->render('transactions/receipt', $this->data);
    }
}
