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

/**
 * Super Admin Portals Controller
 */
class Portals extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('superadmin/Portal_model');
        $this->load->model('superadmin/Super_admin_model');
        $this->_check_super_admin();
    }

    private function _check_super_admin() {
        if ($this->session->userdata('super_admin_logged_in') !== TRUE) {
            redirect('superadmin/auth/login');
        }
    }

    public function index() {
        $data['title'] = 'Manage Portals';
        $data['current_page'] = 'portals';
        $data['portals'] = $this->Portal_model->get_all_with_stats();

        $this->load->view('superadmin/layouts/header', $data);
        $this->load->view('superadmin/portals/index', $data);
        $this->load->view('superadmin/layouts/footer');
    }

    public function create() {
        $data['title'] = 'Add New Portal';
        $data['current_page'] = 'portals';

        if ($this->input->method() == 'post') {
            $this->form_validation->set_rules('portal_code', 'Portal Code', 'required|trim|is_unique[portals.portal_code]');
            $this->form_validation->set_rules('portal_name', 'Portal Name', 'required|trim');
            $this->form_validation->set_rules('portal_url', 'Portal URL', 'required|trim');

            if ($this->form_validation->run() === TRUE) {
                $portal_data = [
                    'portal_code' => $this->input->post('portal_code'),
                    'portal_name' => $this->input->post('portal_name'),
                    'portal_url' => $this->input->post('portal_url'),
                    'logo_url' => $this->input->post('logo_url'),
                    'primary_color' => $this->input->post('primary_color') ?: '#007bff',
                    'status' => $this->input->post('status') ?: 'active'
                ];

                $portal_id = $this->Portal_model->create($portal_data);
                
                if ($portal_id) {
                    $this->Super_admin_model->log_activity('portal_created', 'Created portal: ' . $portal_data['portal_name'], $portal_data['portal_code']);
                    $this->session->set_flashdata('success', 'Portal created successfully!');
                    redirect('superadmin/portals');
                } else {
                    $this->session->set_flashdata('error', 'Failed to create portal.');
                }
            }
        }

        $this->load->view('superadmin/layouts/header', $data);
        $this->load->view('superadmin/portals/create', $data);
        $this->load->view('superadmin/layouts/footer');
    }

    public function edit($id) {
        $data['title'] = 'Edit Portal';
        $data['current_page'] = 'portals';
        $data['portal'] = $this->Portal_model->get($id);

        if (!$data['portal']) {
            $this->session->set_flashdata('error', 'Portal not found.');
            redirect('superadmin/portals');
        }

        if ($this->input->method() == 'post') {
            $this->form_validation->set_rules('portal_name', 'Portal Name', 'required|trim');
            $this->form_validation->set_rules('portal_url', 'Portal URL', 'required|trim');

            if ($this->form_validation->run() === TRUE) {
                $portal_data = [
                    'portal_name' => $this->input->post('portal_name'),
                    'portal_url' => $this->input->post('portal_url'),
                    'logo_url' => $this->input->post('logo_url'),
                    'primary_color' => $this->input->post('primary_color') ?: '#007bff',
                    'status' => $this->input->post('status') ?: 'active'
                ];

                if ($this->Portal_model->update($id, $portal_data)) {
                    $this->Super_admin_model->log_activity('portal_updated', 'Updated portal: ' . $portal_data['portal_name'], $data['portal']->code);
                    $this->session->set_flashdata('success', 'Portal updated successfully!');
                    redirect('superadmin/portals');
                } else {
                    $this->session->set_flashdata('error', 'Failed to update portal.');
                }
            }
        }

        $this->load->view('superadmin/layouts/header', $data);
        $this->load->view('superadmin/portals/edit', $data);
        $this->load->view('superadmin/layouts/footer');
    }

    public function delete($id) {
        $portal = $this->Portal_model->get($id);
        
        if (!$portal) {
            $this->session->set_flashdata('error', 'Portal not found.');
            redirect('superadmin/portals');
        }

        if ($this->Portal_model->delete($id)) {
            $this->Super_admin_model->log_activity('portal_deleted', 'Deleted portal: ' . $portal->name, $portal->code);
            $this->session->set_flashdata('success', 'Portal deleted successfully!');
        } else {
            $this->session->set_flashdata('error', 'Failed to delete portal.');
        }

        redirect('superadmin/portals');
    }
}
