-- Create system_settings table for storing application configuration
CREATE TABLE IF NOT EXISTS system_settings (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  key VARCHAR(255) NOT NULL UNIQUE,
  value TEXT,
  category VARCHAR(100) NOT NULL,
  super_req BOOLEAN DEFAULT FALSE,
  created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
  updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);

-- Create index on key for faster lookups
CREATE INDEX IF NOT EXISTS idx_system_settings_key ON system_settings(key);

-- Create index on category for filtering
CREATE INDEX IF NOT EXISTS idx_system_settings_category ON system_settings(category);


-- Insert default settings
INSERT INTO system_settings (key, value, category)
VALUES
  -- SMTP Settings
  ('smtp_host', NULL, 'email', TRUE),
  ('smtp_port', NULL, 'email', TRUE),
  ('smtp_user', NULL, 'email', TRUE),
  ('smtp_password', NULL, 'email', TRUE),
  ('smtp_from_email', NULL, 'email', TRUE),
  ('smtp_from_name', NULL, 'email', FALSE),
  
  -- Site Settings
  ('site_domain', NULL, 'site', TRUE),
  ('site_api_domain', NULL, 'site', TRUE),
  ('site_protocol', NULL, 'site', TRUE),
  ('site_environment', NULL, 'site', TRUE),
  ('site_deployment', NULL, 'site', TRUE),
  ('site_redis_host', NULL, 'site', TRUE),
  ('site_redis_tls', NULL, 'site', TRUE),
  ('site_aws_region', NULL, 'site', TRUE),
  ('site_aws_s3_bucket', NULL, 'site', TRUE),
  ('site_cdn_domain', NULL, 'site', TRUE),
  ('site_aws_queue_url', NULL, 'site', TRUE),
  ('site_read_host', NULL, 'site', TRUE),
  ('site_db_max_connections', NULL, 'site', TRUE),
  ('site_session_secret', NULL, 'site', TRUE),
  ('site_redis_port', NULL, 'site', TRUE),
  ('site_redis_password', NULL, 'site', TRUE),
  
  -- Payment Settings
  ('currency', 'CAD', 'payment', FALSE),
  ('tax_rate', '0', 'payment', FALSE),
  
  -- Shipping Settings
  ('shipping_flat_rate', '10.00', 'shipping', FALSE),
  ('shipping_free_threshold', '50.00', 'shipping', FALSE),
  ('shipping_enabled', 'true', 'shipping', FALSE),
  ('easypost_api_key', NULL, 'shipping', TRUE),
  ('easypost_enabled', 'false', 'shipping', FALSE),
  ('shipping_origin_street', '123 Main St', 'shipping', FALSE),
  ('shipping_origin_city', 'Vancouver', 'shipping', FALSE),
  ('shipping_origin_state', 'BC', 'shipping', FALSE),
  ('shipping_origin_zip', 'V6K 1V6', 'shipping', FALSE),
  ('shipping_origin_country', 'CA', 'shipping', FALSE),
  ('shipping_default_package_length', '15', 'shipping', FALSE),
  ('shipping_default_package_width', '12', 'shipping', FALSE),
  ('shipping_default_package_height', '10', 'shipping', FALSE),
  ('shipping_default_package_unit', 'cm', 'shipping', FALSE),
  ('shipping_default_package_weight_unit', 'g', 'shipping', FALSE),
  ('shipping_carriers_allowed', 'USPS,UPS,FedEx,DHL,Canada Post,Purolator', 'shipping', FALSE)
ON CONFLICT (key) DO NOTHING;