-- 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,
  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'),
  ('smtp_port', NULL, 'email'),
  ('smtp_user', NULL, 'email'),
  ('smtp_password', NULL, 'email'),
  ('smtp_from_email', NULL, 'email'),
  ('smtp_from_name', NULL, 'email'),
  
  -- Site Settings
  -- ('site_name', NULL, 'site'),
  ('site_domain', NULL, 'site'),
  ('site_api_domain', NULL, 'site'),
  ('site_protocol', NULL, 'site'),
  ('site_environment', NULL, 'site'),
  
  -- Payment Settings
  ('currency', 'CAD', 'payment'),
  ('tax_rate', '0', 'payment'),
  
  -- Shipping Settings
  ('shipping_flat_rate', '10.00', 'shipping'),
  ('shipping_free_threshold', '50.00', 'shipping'),
  ('shipping_enabled', 'true', 'shipping'),
  ('easypost_api_key', NULL, 'shipping'),
  ('easypost_enabled', 'false', 'shipping'),
  ('shipping_origin_street', '123 Main St', 'shipping'),
  ('shipping_origin_city', 'Vancouver', 'shipping'),
  ('shipping_origin_state', 'BC', 'shipping'),
  ('shipping_origin_zip', 'V6K 1V6', 'shipping'),
  ('shipping_origin_country', 'CA', 'shipping'),
  ('shipping_default_package_length', '15', 'shipping'),
  ('shipping_default_package_width', '12', 'shipping'),
  ('shipping_default_package_height', '10', 'shipping'),
  ('shipping_default_package_unit', 'cm', 'shipping'),
  ('shipping_default_weight_unit', 'g', 'shipping'),
  ('shipping_carriers_allowed', 'USPS,UPS,FedEx,DHL,Canada Post,Purolator', 'shipping')
ON CONFLICT (key) DO NOTHING;