-- CLOUD_DB Schema for local development
-- Auto-generated from production

CREATE TABLE IF NOT EXISTS providers (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  name TEXT NOT NULL UNIQUE,
  display_name TEXT NOT NULL,
  api_base_url TEXT,
  last_sync_at TEXT,
  sync_status TEXT NOT NULL DEFAULT 'pending' CHECK (sync_status IN ('pending', 'syncing', 'success', 'error')),
  sync_error TEXT,
  created_at TEXT NOT NULL DEFAULT (datetime('now')),
  updated_at TEXT NOT NULL DEFAULT (datetime('now'))
);

CREATE TABLE IF NOT EXISTS regions (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  provider_id INTEGER NOT NULL,
  region_code TEXT NOT NULL,
  region_name TEXT NOT NULL,
  country_code TEXT,
  latitude REAL,
  longitude REAL,
  available INTEGER NOT NULL DEFAULT 1,
  created_at TEXT NOT NULL DEFAULT (datetime('now')),
  updated_at TEXT NOT NULL DEFAULT (datetime('now')),
  FOREIGN KEY (provider_id) REFERENCES providers(id) ON DELETE CASCADE,
  UNIQUE(provider_id, region_code)
);

CREATE TABLE IF NOT EXISTS instance_types (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  provider_id INTEGER NOT NULL,
  instance_id TEXT NOT NULL,
  instance_name TEXT NOT NULL,
  vcpu INTEGER NOT NULL,
  memory_mb INTEGER NOT NULL,
  storage_gb INTEGER NOT NULL,
  transfer_tb REAL,
  network_speed_gbps REAL,
  gpu_count INTEGER DEFAULT 0,
  gpu_type TEXT,
  instance_family TEXT CHECK (instance_family IN ('general', 'compute', 'memory', 'storage', 'gpu')),
  metadata TEXT,
  created_at TEXT NOT NULL DEFAULT (datetime('now')),
  updated_at TEXT NOT NULL DEFAULT (datetime('now')),
  FOREIGN KEY (provider_id) REFERENCES providers(id) ON DELETE CASCADE,
  UNIQUE(provider_id, instance_id)
);

CREATE TABLE IF NOT EXISTS pricing (
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  instance_type_id INTEGER NOT NULL,
  region_id INTEGER NOT NULL,
  hourly_price REAL NOT NULL,
  monthly_price REAL NOT NULL,
  currency TEXT NOT NULL DEFAULT 'USD',
  available INTEGER NOT NULL DEFAULT 1,
  created_at TEXT NOT NULL DEFAULT (datetime('now')),
  updated_at TEXT NOT NULL DEFAULT (datetime('now')),
  hourly_price_krw REAL,
  monthly_price_krw REAL,
  hourly_price_retail REAL,
  monthly_price_retail REAL,
  FOREIGN KEY (instance_type_id) REFERENCES instance_types(id) ON DELETE CASCADE,
  FOREIGN KEY (region_id) REFERENCES regions(id) ON DELETE CASCADE,
  UNIQUE(instance_type_id, region_id)
);

-- Seed test data
INSERT OR IGNORE INTO providers (id, name, display_name, api_base_url, sync_status) VALUES
  (1, 'linode', 'Linode (Akamai)', 'https://api.linode.com/v4', 'success'),
  (2, 'vultr', 'Vultr', 'https://api.vultr.com/v2', 'success');

INSERT OR IGNORE INTO regions (id, provider_id, region_code, region_name, country_code, available) VALUES
  (1, 1, 'ap-northeast', 'Tokyo 2, JP', 'JP', 1),
  (2, 1, 'ap-south', 'Singapore, SG', 'SG', 1),
  (3, 2, 'nrt', 'Tokyo', 'JP', 1),
  (4, 2, 'sgp', 'Singapore', 'SG', 1);

INSERT OR IGNORE INTO instance_types (id, provider_id, instance_id, instance_name, vcpu, memory_mb, storage_gb, transfer_tb, instance_family) VALUES
  (1, 1, 'g6-nanode-1', 'Nanode 1GB', 1, 1024, 25, 1, 'general'),
  (2, 1, 'g6-standard-1', 'Linode 2GB', 1, 2048, 50, 2, 'general'),
  (3, 1, 'g6-standard-2', 'Linode 4GB', 2, 4096, 80, 4, 'general'),
  (4, 2, 'vc2-1c-1gb', 'Cloud Compute 1GB', 1, 1024, 25, 1, 'general'),
  (5, 2, 'vc2-1c-2gb', 'Cloud Compute 2GB', 1, 2048, 55, 2, 'general'),
  (6, 2, 'vc2-2c-4gb', 'Cloud Compute 4GB', 2, 4096, 80, 3, 'general');

INSERT OR IGNORE INTO pricing (id, instance_type_id, region_id, hourly_price, monthly_price, monthly_price_krw, available) VALUES
  (1, 1, 1, 0.0075, 5.0, 7500, 1),
  (2, 2, 1, 0.018, 12.0, 18000, 1),
  (3, 3, 1, 0.036, 24.0, 36000, 1),
  (4, 1, 2, 0.0075, 5.0, 7500, 1),
  (5, 4, 3, 0.007, 5.0, 7500, 1),
  (6, 5, 3, 0.015, 10.0, 15000, 1),
  (7, 6, 3, 0.03, 20.0, 30000, 1),
  (8, 4, 4, 0.007, 5.0, 7500, 1);
