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

class Home extends CI_Controller {

    public function __construct()
    {
        parent::__construct();

        // Root (bina tenant slug) par clinic login NAHI — super-admin dashboard bhejo.
        // /ammarah/admin, /alkhidmat/admin wale slug ke sath hi clinic login chalta hai.
        if (method_exists($this->router, 'get_tenant_slug') && $this->router->get_tenant_slug() === '') {
            redirect('/', 'refresh');
        }

        $this->load->model('User_model', 'user');
        $this->load->model('Payment_model', 'payment');
        $this->load->model('Settings_model', 'setup');

    }

    public function index()
    {
        if (isset($_SESSION['qurbani_user_id'])) {
            redirect('/admin/home/dashboard','refresh');
        }
        $data['title'] = 'Dashboard';
        $data['settings'] = $this->setup->get_by_id(1);
        $this->load->view('admin/index', $data);
        
    }

    public function validate()
    {
        $login    = trim((string)$this->input->post('email'));
        $password = (string)$this->input->post('password');

        // Login by email OR username
        $user = $this->user->find_by_email_or_username($login);

        if ($user && User_model::verify_password($password, $user->password))
        {
            // Legacy MD5 hash ko bcrypt pe upgrade karo (kamyab login par)
            if (password_needs_rehash($user->password, PASSWORD_BCRYPT)) {
                $this->user->update($user->id, array('password' => User_model::hash_password($password)));
            }

            $user_data = [
                'qurbani_fullname' => $user->name,
                'qurbani_user_id' => $user->id,
                'qurbani_role' => $user->role,
                'qurbani_is_logged_in' => TRUE
            ];

            $this->session->set_userdata($user_data);

            redirect('/admin/home/dashboard','refresh');
        }
        else
        {
            $data['error'] = "Sorry! Your Credentials didn't match our records";
			$data['settings'] = $this->setup->get_by_id(1);
            $this->load->view('admin/index', $data);
        }
    }

    public function logout()
    {
        $this->session->sess_destroy();
        redirect('/admin','refresh');
    }
    
    public function change_year($year)
    {
        $this->session->set_userdata(['qurbani_year' => $year]);
        $this->load->library('user_agent');
        redirect($this->agent->referrer());
        // redirect('/admin/home/dashboard');
    }

    public function dashboard($year = null)
    {
        if (!isset($_SESSION['qurbani_user_id'])) {
            redirect('/admin/','refresh');
        }
        
        if(!isset($_SESSION['qurbani_year'])) {
            $this->session->set_userdata(['qurbani_year' => date('Y')]);
        }
        $data['title'] = 'Dashboard';
        $data['settings'] = $this->setup->get_by_id(1);
        $data['mainContent'] = 'admin/dashboard';
        $this->load->view('admin/layout/master', $data);
        
    }
    
    public function getUserDailyCash()
    {
        if (!isset($_SESSION['qurbani_user_id'])) {
            redirect('/admin/','refresh');
        }

        $this->load->model('Payment_model', 'payment');
        $user_id = $_SESSION['qurbani_user_id'];
        // get_total_cash_by_user() user-wise today's cash returns (debit - credit)
        echo $this->payment->get_total_cash_by_user($user_id, true);
    }



}
