-- Migration 001 Rollback
-- Purpose: Revert schema enhancements to original state
-- Date: 2026-01-19
-- WARNING: This will remove CHECK constraints and audit_logs table

-- =============================================================================
-- ROLLBACK STEP 1: user_deposits - Remove CHECK constraint
-- =============================================================================

-- 1.1 Create backup table
CREATE TABLE user_deposits_backup AS SELECT * FROM user_deposits;

-- 1.2 Drop existing table
DROP TABLE user_deposits;

-- 1.3 Recreate table without CHECK constraint
CREATE TABLE user_deposits (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  user_id INTEGER NOT NULL UNIQUE,
  balance INTEGER NOT NULL DEFAULT 0,
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (user_id) REFERENCES users(id)
);

-- 1.4 Restore all data
INSERT INTO user_deposits (id, user_id, balance, created_at, updated_at)
SELECT id, user_id, balance, created_at, updated_at
FROM user_deposits_backup;

-- 1.5 Drop backup table
DROP TABLE user_deposits_backup;

-- 1.6 Recreate index
CREATE INDEX IF NOT EXISTS idx_deposits_user ON user_deposits(user_id);

-- =============================================================================
-- ROLLBACK STEP 2: deposit_transactions - Remove length constraint
-- =============================================================================

-- 2.1 Create backup table
CREATE TABLE deposit_transactions_backup AS SELECT * FROM deposit_transactions;

-- 2.2 Drop existing table
DROP TABLE deposit_transactions;

-- 2.3 Recreate table without length constraint
CREATE TABLE deposit_transactions (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  user_id INTEGER NOT NULL,
  type TEXT NOT NULL CHECK(type IN ('deposit', 'withdrawal', 'refund')),
  amount INTEGER NOT NULL,
  status TEXT NOT NULL DEFAULT 'pending' CHECK(status IN ('pending', 'confirmed', 'rejected', 'cancelled')),
  depositor_name TEXT,
  description TEXT,
  confirmed_at DATETIME,
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (user_id) REFERENCES users(id)
);

-- 2.4 Restore all data
INSERT INTO deposit_transactions (
  id, user_id, type, amount, status, depositor_name, description, confirmed_at, created_at
)
SELECT
  id, user_id, type, amount, status, depositor_name, description, confirmed_at, created_at
FROM deposit_transactions_backup;

-- 2.5 Drop backup table
DROP TABLE deposit_transactions_backup;

-- 2.6 Recreate indexes
CREATE INDEX IF NOT EXISTS idx_transactions_user ON deposit_transactions(user_id);
CREATE INDEX IF NOT EXISTS idx_transactions_status ON deposit_transactions(status, created_at DESC);

-- =============================================================================
-- ROLLBACK STEP 3: Remove audit_logs table
-- =============================================================================

-- 3.1 Drop indexes
DROP INDEX IF EXISTS idx_audit_logs_user_id;
DROP INDEX IF EXISTS idx_audit_logs_telegram_id;
DROP INDEX IF EXISTS idx_audit_logs_action;
DROP INDEX IF EXISTS idx_audit_logs_resource;
DROP INDEX IF EXISTS idx_audit_logs_created_at;

-- 3.2 Drop table
DROP TABLE IF EXISTS audit_logs;

-- =============================================================================
-- VERIFICATION QUERIES
-- =============================================================================

-- Count records in each table
SELECT 'users' as table_name, COUNT(*) as count FROM users
UNION ALL
SELECT 'user_deposits', COUNT(*) FROM user_deposits
UNION ALL
SELECT 'deposit_transactions', COUNT(*) FROM deposit_transactions;

-- Verify audit_logs is gone
SELECT
  CASE
    WHEN COUNT(*) = 0 THEN 'audit_logs successfully removed'
    ELSE 'WARNING: audit_logs still exists'
  END as verification_result
FROM sqlite_master
WHERE type='table' AND name='audit_logs';

-- =============================================================================
-- ROLLBACK COMPLETE
-- =============================================================================

SELECT 'Migration 001 rollback completed successfully' as status, datetime('now') as timestamp;
