<?php

// Prevent direct access to the script
defined('BASEPATH') || exit('No direct script access allowed');

// Enable superadmin
update_option('superadmin_enabled', 1);

// Get CodeIgniter instance
$CI = &get_instance();
if (APP_DB_NAME != $CI->db->database) {
    return false;
}

// Enable tenants' landing page
add_option('tenants_landing', 1);

// Install email templates for superadmin
installSuperadminEmailTemplates();

// Allow registration of new tenants
add_option('allow_registration', 0);

// Enable newly registered tenants by default
add_option('email_verification_require_after_tenant_register', 0);

// Enable customers register require confirmation by default
update_option('customers_register_require_confirmation', 0);

/*
 * Create table section
 */
if (!$CI->db->table_exists('plan_management')) {
    $CI->db->query('CREATE TABLE `'.db_prefix().'plan_management` (
            `id` int(11) NOT NULL AUTO_INCREMENT,
            `plan_name` varchar(255) NOT NULL,
            `plan_description` text ,
            `plan_image` text NOT NULL,
            `price` decimal(10,'.get_decimal_places().') NOT NULL,
            `trial` TINYINT(1) NOT NULL DEFAULT "0",
            `most_popular` TINYINT(1) NOT NULL DEFAULT "0",
            `limitations` TEXT NOT NULL,
            `allowed_payment_modes` mediumtext,
            `taxes` varchar(255) NOT NULL,
            `recurring` int(11) NOT NULL DEFAULT "0",
            `recurring_type` varchar(10) NOT NULL,
            `custom_recurring` tinyint(1) NOT NULL DEFAULT "0",
            `cycles` int(11) NOT NULL DEFAULT "0",
            PRIMARY KEY (`id`)
        ) ENGINE=InnoDB DEFAULT CHARSET='.$CI->db->char_set.';');
}

if (!$CI->db->table_exists(db_prefix().'cron_data')) {
    $CI->db->query('CREATE TABLE `'.db_prefix().'cron_data` (
            `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
            `called_url` VARCHAR(255) NOT NULL,
            `response` TEXT NOT NULL,
            `tenant_id` INT NOT NULL ,
            `execution_time` DATETIME NOT NULL,
            PRIMARY KEY (`id`)
        ) ENGINE=InnoDB DEFAULT CHARSET='.$CI->db->char_set.';');
}

if (!$CI->db->table_exists(db_prefix().'client_plan')) {
    $CI->db->query('CREATE TABLE `'.db_prefix().'client_plan` (
            `id` int(11) NOT NULL AUTO_INCREMENT,
            `userid` int(11) NOT NULL COMMENT "client id",
            `tenants_name` varchar(191) NOT NULL,
            `tenants_db` varchar(100) NOT NULL,
            `tenants_db_username` varchar(100) NOT NULL,
            `tenants_db_password` varchar(255) NOT NULL,
            `tenants_admin` int(11) NOT NULL COMMENT "client contact",
            `plan_id` int(11) NOT NULL,
            `plan_details_json` longtext NOT NULL,
            `trial_days` int(3) NOT NULL,
            `trial_start_time` DATETIME NOT NULL,
            `is_invoiced` TINYINT(1) NOT NULL DEFAULT "0" COMMENT "if invoiced it will store first invoice id",
            `is_active` TINYINT(1) NOT NULL DEFAULT "0",
            `inactive_date` varchar(150) DEFAULT NULL,
            `is_deleted` TINYINT(1) NOT NULL DEFAULT "0",
            PRIMARY KEY (`id`)
        ) ENGINE=InnoDB DEFAULT CHARSET='.$CI->db->char_set.';');
}

if (!$CI->db->table_exists(db_prefix().'saas_activity_log')) {
    $CI->db->query('CREATE TABLE `'.db_prefix().'saas_activity_log` (
            `id` int(11) NOT NULL AUTO_INCREMENT,
            `description` text NOT NULL,
            `recorded_at` datetime NOT NULL,
            `staffid` varchar(100) DEFAULT NULL,
            PRIMARY KEY (`id`)
        ) ENGINE=InnoDB DEFAULT CHARSET='.$CI->db->char_set.';');
}

$my_files_list = [
    APPPATH.'helpers/my_functions_helper.php'      => module_dir_path(SUPERADMIN_MODULE, '/resources/application/helpers/my_functions_helper.php'),
    VIEWPATH.'themes/perfex/views/my_register.php' => module_dir_path(SUPERADMIN_MODULE, '/resources/application/views/themes/perfex/views/my_register.php'),
    VIEWPATH.'admin/modules/my_list.php'           => module_dir_path(SUPERADMIN_MODULE, '/resources/application/views/admin/modules/my_list.php'),
];

// Copy each file in $my_files_list to its actual path if it doesn't already exist
foreach ($my_files_list as $actual_path => $resource_path) {
    if (!file_exists($actual_path)) {
        copy($resource_path, $actual_path);
    }
}

// An array of files to backup
$backup_files_list = [
    APPPATH.'helpers/clients_helper.php' => module_dir_path(SUPERADMIN_MODULE, '/resources/application/helpers/clients_helper.php'),
    APPPATH.'helpers/files_helper.php'   => module_dir_path(SUPERADMIN_MODULE, '/resources/application/helpers/files_helper.php'),
    APPPATH.'helpers/staff_helper.php'   => module_dir_path(SUPERADMIN_MODULE, '/resources/application/helpers/staff_helper.php'),
    APPPATH.'helpers/upload_helper.php'  => module_dir_path(SUPERADMIN_MODULE, '/resources/application/helpers/upload_helper.php'),
];

// Backup each file in $backup_files_list by renaming it with a '.backup' suffix if it exists, then copy the new version from the resources directory
foreach ($backup_files_list as $actual_path => $resource_path) {
    if (file_exists($actual_path)) {
        rename($actual_path, $actual_path.'.backup');
    }
    if (!file_exists($actual_path)) {
        copy($resource_path, $actual_path);
    }
}
/* End of file install.php */
