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

class Migration_Version_618 extends CI_Migration
{
    public function __construct()
    {
        parent::__construct();
    }

    public function up()
    {
        if ($this->db->table_exists('tbl_salary_deduction')) {
            if (!$this->db->field_exists('deduction_percent', 'tbl_salary_deduction')) {
                $this->db->query("ALTER TABLE `tbl_salary_deduction` ADD `deduction_percent` float(18,2) DEFAULT NULL AFTER `deduction_value`");
            }

            if (!$this->db->field_exists('deduction_type', 'tbl_salary_deduction')) {
                $this->db->query("ALTER TABLE `tbl_salary_deduction` ADD `deduction_type` varchar(50) DEFAULT NULL AFTER `deduction_percent`");
            }

            $this->db->query("UPDATE `tbl_salary_deduction` SET `deduction_percent` = `deduction_value` WHERE `deduction_percent` IS NULL");
            $this->db->query("UPDATE `tbl_salary_deduction` SET `deduction_type` = 'fixed' WHERE `deduction_type` IS NULL OR `deduction_type` = ''");
        }

		// remember_token,remember_expire,sort_user fields added in tbl_users table in version 6.1.8 but these fields are not added in some of the previous versions like 6.1.3, 6.1.4, 6.1.5, 6.1.6, and 6.1.7 so we need to add these fields if not exists to avoid any issue in future when we will update to version 6.1.8 from any of the previous versions.
		if ($this->db->table_exists('tbl_users')) {
			if (!$this->db->field_exists('remember_token', 'tbl_users')) {
				$this->db->query("ALTER TABLE `tbl_users` ADD `remember_token` varchar(255) DEFAULT NULL AFTER `media_path_slug`");
			}

			if (!$this->db->field_exists('remember_expire', 'tbl_users')) {
				$this->db->query("ALTER TABLE `tbl_users` ADD `remember_expire` datetime DEFAULT NULL AFTER `remember_token`");
			}

			if (!$this->db->field_exists('sort_user', 'tbl_users')) {
				$this->db->query("ALTER TABLE `tbl_users` ADD `sort_user` int(11) NOT NULL DEFAULT '0' AFTER `remember_expire`");
			}
		}

        $this->update_sidebar_lucide_icons();

        if ($this->db->table_exists('tbl_config')) {
            $this->db->where('config_key', 'version')->update('tbl_config', array('value' => '6.1.8'));
        }
    }

    public function down()
    {
        if ($this->db->table_exists('tbl_salary_deduction')) {
            if ($this->db->field_exists('deduction_type', 'tbl_salary_deduction')) {
                $this->db->query("ALTER TABLE `tbl_salary_deduction` DROP COLUMN `deduction_type`");
            }

            if ($this->db->field_exists('deduction_percent', 'tbl_salary_deduction')) {
                $this->db->query("ALTER TABLE `tbl_salary_deduction` DROP COLUMN `deduction_percent`");
            }
        }

        if ($this->db->table_exists('tbl_config')) {
            $this->db->where('config_key', 'version')->update('tbl_config', array('value' => '6.1.7'));
        }
    }

    private function update_sidebar_lucide_icons()
    {
        if (!$this->db->table_exists('tbl_menu') || !$this->db->field_exists('icon', 'tbl_menu')) {
            return;
        }

        $this->db->query("
            UPDATE `tbl_menu`
            SET `icon` = CASE `label`
                WHEN 'warning' THEN 'shield-alert'
                WHEN 'promotion' THEN 'gallery-horizontal-end'
                WHEN 'departments' THEN 'users'
                WHEN 'time_history' THEN 'clock'
                WHEN 'timechange_request' THEN 'calendar'
                WHEN 'hourly_rate' THEN 'clock'
                WHEN 'employee_salary_list' THEN 'users'
                WHEN 'overtime' THEN 'clock'
                WHEN 'performance_report' THEN 'calendar'
                WHEN 'training' THEN 'train-front'
                WHEN 'announcements' THEN 'megaphone'
                WHEN 'holiday' THEN 'calendar-plus'
                WHEN 'company_details' THEN 'building-2'
                WHEN 'system_settings' THEN 'monitor-cog'
                WHEN 'email_templates' THEN 'file-pen-line'
                WHEN 'leave_category' THEN 'calendar-minus'
                WHEN 'income_category' THEN 'badge-dollar-sign'
                WHEN 'custom_field' THEN 'list-plus'
                WHEN 'cronjob' THEN 'timer-reset'
                WHEN 'notification' THEN 'bell'
                WHEN 'email_notification' THEN 'mail-check'
                WHEN 'translations' THEN 'languages'
                WHEN 'system_update' THEN 'refresh-cw'
                ELSE `icon`
            END
            WHERE `label` IN (
                'warning',
                'promotion',
                'departments',
                'time_history',
                'timechange_request',
                'hourly_rate',
                'employee_salary_list',
                'overtime',
                'performance_report',
                'training',
                'announcements',
                'holiday',
                'company_details',
                'system_settings',
                'email_templates',
                'leave_category',
                'income_category',
                'custom_field',
                'cronjob',
                'notification',
                'email_notification',
                'translations',
                'system_update'
            )
        ");
    }
}
