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

class Accounts extends Auth_Controller {

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

    /**
     * List user accounts
     */
    public function index() {
        
        $accounts = $this->Account_model->get_by_user($this->user->id);
        
        $this->data['title'] = 'My Accounts';
        $this->data['accounts'] = $accounts;
        
        $this->render('accounts/index', $this->data);
    }

    /**
     * View account details
     */
    public function view($id) {
        $account = $this->Account_model->get($id);
        
        if (!$account || $account->user_id != $this->user->id) {
            show_404();
            return;
        }
        
        // Get wallet
        $wallet = $this->Wallet_model->get_by_account($id, $account->currency);
        
        // Get recent transactions
        $transactions = $this->Transaction_model->get_by_account($id, 20);
        
        // Get account tier
        $tier = $this->Account_model->get_tier($account->tier_id);
        
        $this->data['title'] = 'Account Details';
        $this->data['account'] = $account;
        $this->data['wallet'] = $wallet;
        $this->data['transactions'] = $transactions;
        $this->data['tier'] = $tier;
        
        $this->render('accounts/view', $this->data);
    }

    /**
     * Set primary account
     */
    public function set_primary($id) {
        $account = $this->Account_model->get($id);
        
        if (!$account || $account->user_id != $this->user->id) {
            $this->error('Account not found');
            return;
        }
        
        $this->Account_model->set_primary($this->user->id, $id);
        
        $this->success('Primary account updated');
    }

    /**
     * Get account statement
     */
    public function statement($id) {
        $account = $this->Account_model->get($id);
        
        if (!$account || $account->user_id != $this->user->id) {
            show_404();
            return;
        }
        
        $date_from = $this->input->get('from') ?: date('Y-m-01');
        $date_to = $this->input->get('to') ?: date('Y-m-d');
        
        $transactions = $this->Transaction_model->get_by_account($id, 1000, 0);
        
        // Filter by date
        $transactions = array_filter($transactions, function($t) use ($date_from, $date_to) {
            return $t->created_at >= $date_from && $t->created_at <= $date_to . ' 23:59:59';
        });
        
        $this->data['title'] = 'Account Statement';
        $this->data['account'] = $account;
        $this->data['transactions'] = $transactions;
        $this->data['date_from'] = $date_from;
        $this->data['date_to'] = $date_to;
        
        $this->render('accounts/statement', $this->data);
    }

    /**
     * Download account statement as PDF
     */
    public function download_statement($id) {
        $account = $this->Account_model->get($id);
        
        if (!$account || $account->user_id != $this->user->id) {
            show_404();
            return;
        }
        
        $date_from = $this->input->get('from') ?: date('Y-m-01');
        $date_to = $this->input->get('to') ?: date('Y-m-d');
        
        $transactions = $this->Transaction_model->get_by_account($id, 1000, 0);
        
        // Filter by date
        $transactions = array_filter($transactions, function($t) use ($date_from, $date_to) {
            return $t->created_at >= $date_from && $t->created_at <= $date_to . ' 23:59:59';
        });
        
        $data['account'] = $account;
        $data['transactions'] = array_values($transactions);
        $data['date_from'] = $date_from;
        $data['date_to'] = $date_to;
        
        $this->load->view('accounts/statement_pdf', $data);
    }

    /**
     * Get account balance (AJAX)
     */
    public function balance($id = null) {
        if ($id) {
            $account = $this->Account_model->get($id);
            
            if (!$account || $account->user_id != $this->user->id) {
                $this->error('Account not found');
                return;
            }
            
            $wallet = $this->Wallet_model->get_by_account($id, $account->currency);
            
            $this->success('Balance retrieved', [
                'account_number' => $account->account_number,
                'ledger_balance' => $wallet->ledger_balance,
                'available_balance' => $wallet->available_balance,
                'currency' => $account->currency
            ]);
        } else {
            // Get total balance
            $total = $this->Account_model->get_total_balance($this->user->id);
            
            $this->success('Total balance retrieved', [
                'total_balance' => $total,
                'currency' => 'USD'
            ]);
        }
    }
}
