-- Migration script for coconut-leaf database v1 to v2
-- This script updates field names by:
-- 1. Removing 'ccn_' prefix from all fields
-- 2. Converting camelCase to snake_case

-- Step 1: Rename user table columns
ALTER TABLE user RENAME COLUMN ccn_name TO name;
ALTER TABLE user RENAME COLUMN ccn_password TO password;
ALTER TABLE user RENAME COLUMN ccn_isAdmin TO is_admin;
ALTER TABLE user RENAME COLUMN ccn_salt TO salt;

-- Step 2: Rename token table columns
ALTER TABLE token RENAME COLUMN ccn_user TO user;
ALTER TABLE token RENAME COLUMN ccn_token TO token;
ALTER TABLE token RENAME COLUMN ccn_tokenExpireOn TO token_expire_on;
ALTER TABLE token RENAME COLUMN ccn_ua TO ua;
ALTER TABLE token RENAME COLUMN ccn_ip TO ip;

-- Step 3: Rename collection table columns
ALTER TABLE collection RENAME COLUMN ccn_uuid TO uuid;
ALTER TABLE collection RENAME COLUMN ccn_name TO name;
ALTER TABLE collection RENAME COLUMN ccn_user TO user;
ALTER TABLE collection RENAME COLUMN ccn_lastChange TO last_change;

-- Step 4: Rename share table columns
ALTER TABLE share RENAME COLUMN ccn_uuid TO uuid;
ALTER TABLE share RENAME COLUMN ccn_target TO target;

-- Step 5: Rename calendar table columns
ALTER TABLE calendar RENAME COLUMN ccn_uuid TO uuid;
ALTER TABLE calendar RENAME COLUMN ccn_belongTo TO belong_to;
ALTER TABLE calendar RENAME COLUMN ccn_title TO title;
ALTER TABLE calendar RENAME COLUMN ccn_description TO description;
ALTER TABLE calendar RENAME COLUMN ccn_lastChange TO last_change;
ALTER TABLE calendar RENAME COLUMN ccn_eventDateTimeStart TO event_date_time_start;
ALTER TABLE calendar RENAME COLUMN ccn_eventDateTimeEnd TO event_date_time_end;
ALTER TABLE calendar RENAME COLUMN ccn_timezoneOffset TO timezone_offset;
ALTER TABLE calendar RENAME COLUMN ccn_loopRules TO loop_rules;
ALTER TABLE calendar RENAME COLUMN ccn_loopDateTimeStart TO loop_date_time_start;
ALTER TABLE calendar RENAME COLUMN ccn_loopDateTimeEnd TO loop_date_time_end;

-- Step 6: Rename todo table columns
ALTER TABLE todo RENAME COLUMN ccn_uuid TO uuid;
ALTER TABLE todo RENAME COLUMN ccn_belongTo TO belong_to;
ALTER TABLE todo RENAME COLUMN ccn_data TO data;
ALTER TABLE todo RENAME COLUMN ccn_lastChange TO last_change;

-- Note: Foreign key constraints will be automatically updated by SQLite when renaming columns
-- No additional steps needed for foreign keys
