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

class Cash_register extends MY_Controller {

	public function __construct() {
		parent::__construct();
		$this->load_global();
		$this->load->model('cash_register_model', 'cash_register');
	}

	/**
	 * Cash Register has been removed. Redirect to POS (Customer Display remains in POS).
	 */
	public function index() {
		redirect('pos');
	}

	/**
	 * AJAX: Get status of current user's cash register (open/closed)
	 */
	public function get_status() {
		header('Content-Type: application/json');
		if (!$this->session->userdata('logged_in')) {
			echo json_encode(array('success' => false, 'open' => false, 'message' => 'Not logged in'));
			return;
		}
		if (!$this->cash_register->table_exists()) {
			echo json_encode(array('success' => true, 'open' => true, 'table_missing' => true));
			return;
		}
		$store_id = get_current_store_id();
		$user_id = (int)$this->session->userdata('inv_userid');
		$open = $this->cash_register->get_open_session($store_id, $user_id);
		echo json_encode(array(
			'success' => true,
			'open'    => !empty($open),
			'session' => $open ? array(
				'id'             => $open->id,
				'opened_at'      => $open->opened_at,
				'opening_amount' => $open->opening_amount,
				'account_id'     => isset($open->account_id) ? $open->account_id : null,
				'opening_note'   => $open->opening_note
			) : null
		));
	}

	/**
	 * AJAX: Open register (POST opening_amount, opening_note)
	 */
	public function open_session() {
		header('Content-Type: application/json');
		if (!$this->session->userdata('logged_in')) {
			echo json_encode(array('success' => false, 'message' => 'Not logged in'));
			return;
		}
		$this->permission_check_with_msg('cash_register');
		$amount = $this->input->post('opening_amount');
		$note   = $this->input->post('opening_note') ?: '';
		if ($amount === '' && $amount !== '0') {
			echo json_encode(array('success' => false, 'message' => 'Opening amount is required.'));
			return;
		}
		$amount = (float)str_replace(',', '', $amount);
		$account_id = $this->input->post('account_id') ? (int)$this->input->post('account_id') : null;
		$drawer_total = $this->input->post('drawer_total_at_open');
		if ($drawer_total !== null && $drawer_total !== '') {
			$drawer_total = (float)str_replace(',', '', $drawer_total);
		} else {
			$drawer_total = null;
		}
		$store_id = get_current_store_id();
		$user_id = (int)$this->session->userdata('inv_userid');
		$base = get_base_currency();
		$currency_id = isset($base->id) ? $base->id : null;
		$result = $this->cash_register->open_session($store_id, $user_id, $amount, $note, $currency_id, $account_id, $drawer_total);
		echo json_encode($result);
	}

	/**
	 * AJAX: Update opening amount/account/note for current open session (POST session_id, opening_amount, account_id, opening_note)
	 */
	public function update_opening() {
		header('Content-Type: application/json');
		if (!$this->session->userdata('logged_in')) {
			echo json_encode(array('success' => false, 'message' => 'Not logged in'));
			return;
		}
		$this->permission_check_with_msg('cash_register');
		$session_id = (int)$this->input->post('session_id');
		$amount = $this->input->post('opening_amount');
		$note   = $this->input->post('opening_note') ?: '';
		$account_id = $this->input->post('account_id') ? (int)$this->input->post('account_id') : null;
		$drawer_total = $this->input->post('drawer_total_at_open');
		if ($drawer_total !== null && $drawer_total !== '') {
			$drawer_total = (float)str_replace(',', '', $drawer_total);
		} else {
			$drawer_total = null;
		}
		if ($amount === '' && $amount !== '0') {
			echo json_encode(array('success' => false, 'message' => 'Opening amount is required.'));
			return;
		}
		$amount = (float)str_replace(',', '', $amount);
		$store_id = get_current_store_id();
		$user_id = (int)$this->session->userdata('inv_userid');
		$result = $this->cash_register->update_opening($session_id, $store_id, $user_id, $amount, $account_id, $note, $drawer_total);
		echo json_encode($result);
	}

	/**
	 * AJAX: Close register (POST closing_amount, closing_note)
	 */
	public function close_session() {
		header('Content-Type: application/json');
		if (!$this->session->userdata('logged_in')) {
			echo json_encode(array('success' => false, 'message' => 'Not logged in'));
			return;
		}
		$this->permission_check_with_msg('cash_register');
		$amount = $this->input->post('closing_amount');
		$note   = $this->input->post('closing_note') ?: '';
		if ($amount === '' && $amount !== '0') {
			echo json_encode(array('success' => false, 'message' => 'Closing amount is required.'));
			return;
		}
		$amount = (float)str_replace(',', '', $amount);
		$store_id = get_current_store_id();
		$user_id = (int)$this->session->userdata('inv_userid');
		$result = $this->cash_register->close_session($store_id, $user_id, $amount, $note);
		echo json_encode($result);
	}

	/**
	 * Cash Register removed – redirect to POS.
	 */
	public function variance_report() {
		redirect('pos');
	}

	/**
	 * Cash Register removed – return empty JSON.
	 */
	public function show_variance_report() {
		header('Content-Type: application/json');
		echo json_encode(array('rows' => '', 'grand' => ''));
		return;
	}
}
