-- Deployment 30: Add kpi_form_type to projects + rename jatinder table to accounts

-- 1. Add kpi_form_type column to projects
ALTER TABLE `projects`
  ADD COLUMN IF NOT EXISTS `kpi_form_type`
    ENUM('none','scheduling','ops_manager','accounts')
    NOT NULL DEFAULT 'none'
    COMMENT 'Which KPI form this project uses';

-- 2. Set existing KPI project to scheduling
UPDATE `projects` SET `kpi_form_type` = 'scheduling'
WHERE `code` = 'kpi' OR LOWER(`name`) = 'kpi';

-- 3. Rename jatinder table to accounts (safe: CREATE + rename approach)
--    If kpi_jatinder_entries exists, rename it.
--    If kpi_accounts_entries already exists, skip.
CREATE TABLE IF NOT EXISTS `kpi_accounts_entries` LIKE `kpi_jatinder_entries`;
INSERT IGNORE INTO `kpi_accounts_entries`
  SELECT * FROM `kpi_jatinder_entries`;

CREATE TABLE IF NOT EXISTS `kpi_accounts_review_history` LIKE `kpi_jatinder_review_history`;
INSERT IGNORE INTO `kpi_accounts_review_history`
  SELECT * FROM `kpi_jatinder_review_history`;
