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

$CI = &get_instance();

if (!$CI->db->table_exists(db_prefix().'coi_disclosures')) {
    $CI->db->query('CREATE TABLE `'.db_prefix().'coi_disclosures` (
        `id` INT(11) NOT NULL AUTO_INCREMENT,
        `staff_id` INT(11) NOT NULL,
        `staff_name` VARCHAR(191) NULL,
        `staff_code` VARCHAR(50) NULL,
        `position` VARCHAR(191) NULL,
        `department` VARCHAR(191) NULL,
        `signature` VARCHAR(191) NOT NULL,
        `signed_date` DATE NOT NULL,
        `created_at` DATETIME NULL,
        `updated_at` DATETIME NULL,
        PRIMARY KEY (`id`),
        KEY `staff_id` (`staff_id`),
        KEY `signed_date` (`signed_date`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;');
}

if (!$CI->db->table_exists(db_prefix().'coi_disclosure_items')) {
    $CI->db->query('CREATE TABLE `'.db_prefix().'coi_disclosure_items` (
        `id` INT(11) NOT NULL AUTO_INCREMENT,
        `disclosure_id` INT(11) NOT NULL,
        `question_no` INT(11) NOT NULL,
        `question` TEXT NULL,
        `answer` VARCHAR(10) NULL,
        `details` TEXT NULL,
        PRIMARY KEY (`id`),
        KEY `disclosure_id` (`disclosure_id`),
        KEY `question_no` (`question_no`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;');
}

if (!$CI->db->table_exists(db_prefix().'coi_disclosure_files')) {
    $CI->db->query('CREATE TABLE `'.db_prefix().'coi_disclosure_files` (
        `id` INT(11) NOT NULL AUTO_INCREMENT,
        `disclosure_id` INT(11) NOT NULL,
        `staff_id` INT(11) NOT NULL,
        `file_name` VARCHAR(191) NOT NULL,
        `original_name` VARCHAR(191) NULL,
        `filetype` VARCHAR(100) NULL,
        `filesize` VARCHAR(50) NULL,
        `created_at` DATETIME NULL,
        PRIMARY KEY (`id`),
        KEY `disclosure_id` (`disclosure_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;');
}

// recipients options (role/staff)
if (function_exists('add_option')) {
    if (get_option('coi_notify_role_ids') === null) add_option('coi_notify_role_ids', '');
    if (get_option('coi_notify_staff_ids') === null) add_option('coi_notify_staff_ids', '');
}
