-- =============================================================================
-- Landing E2E Test Database Seed
-- =============================================================================
--
-- This file initializes the database with test data for E2E tests.
-- Tables are created by TypeORM synchronize or migrations.
--
-- Run order:
-- 1. PostgreSQL creates empty database
-- 2. TypeORM synchronize creates tables from entities
-- 3. This seed populates test data
--
-- =============================================================================

-- =============================================================================
-- 1. Create Enums (if not exists via synchronize)
-- =============================================================================

-- Vote transaction type
DO $$ BEGIN
  CREATE TYPE "vote_transaction_type" AS ENUM ('purchase', 'allocate', 'deallocate', 'admin_grant', 'admin_revoke');
EXCEPTION WHEN duplicate_object THEN NULL;
END $$;

-- Shop product type
DO $$ BEGIN
  CREATE TYPE "shop_product_type" AS ENUM ('physical_merchandise', 'physical_accessory', 'digital_product', 'gift_card');
EXCEPTION WHEN duplicate_object THEN NULL;
END $$;

-- Product status
DO $$ BEGIN
  CREATE TYPE "product_status" AS ENUM ('draft', 'coming_soon', 'available', 'out_of_stock', 'archived');
EXCEPTION WHEN duplicate_object THEN NULL;
END $$;

-- Inventory type
DO $$ BEGIN
  CREATE TYPE "inventory_type" AS ENUM ('unlimited', 'limited', 'unique');
EXCEPTION WHEN duplicate_object THEN NULL;
END $$;

-- Variant type
DO $$ BEGIN
  CREATE TYPE "variant_type" AS ENUM ('size', 'color', 'style', 'custom');
EXCEPTION WHEN duplicate_object THEN NULL;
END $$;

-- =============================================================================
-- 2. Translation Locales (supported languages)
-- =============================================================================

CREATE TABLE IF NOT EXISTS translation_locales (
  id SERIAL PRIMARY KEY,
  code VARCHAR(10) UNIQUE NOT NULL,
  name VARCHAR(100) NOT NULL,
  "nativeName" VARCHAR(100) NOT NULL,
  flag VARCHAR(10),
  rtl BOOLEAN DEFAULT FALSE,
  enabled BOOLEAN DEFAULT TRUE,
  "createdAt" TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);

INSERT INTO translation_locales (code, name, "nativeName", flag, rtl, enabled) VALUES
  ('en', 'English', 'English', '🇺🇸', false, true),
  ('es', 'Spanish', 'Español', '🇪🇸', false, true),
  ('fr', 'French', 'Français', '🇫🇷', false, true),
  ('de', 'German', 'Deutsch', '🇩🇪', false, true),
  ('ja', 'Japanese', '日本語', '🇯🇵', false, true),
  ('is', 'Icelandic', 'Íslenska', '🇮🇸', false, true)
ON CONFLICT (code) DO NOTHING;

-- =============================================================================
-- 3. Shop Products (Gift Cards and Test Products)
-- =============================================================================

CREATE TABLE IF NOT EXISTS shop_products (
  id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
  sku VARCHAR(100) NOT NULL UNIQUE,
  name VARCHAR(255) NOT NULL,
  description TEXT,
  long_description TEXT,
  product_type shop_product_type NOT NULL,
  category VARCHAR(100),
  tags TEXT,
  base_price_usd DECIMAL(10, 2),
  min_price_usd DECIMAL(10, 2),
  max_price_usd DECIMAL(10, 2),
  allow_custom_amount BOOLEAN DEFAULT FALSE,
  base_price_tokens INTEGER,
  inventory_type inventory_type NOT NULL DEFAULT 'unlimited',
  inventory_quantity INTEGER,
  inventory_reserved INTEGER NOT NULL DEFAULT 0,
  low_stock_threshold INTEGER,
  status product_status NOT NULL DEFAULT 'draft',
  featured BOOLEAN NOT NULL DEFAULT FALSE,
  sort_order INTEGER NOT NULL DEFAULT 0,
  requires_shipping BOOLEAN NOT NULL DEFAULT FALSE,
  weight_grams INTEGER,
  thumbnail_url VARCHAR(500),
  preview_images TEXT,
  total_sales INTEGER NOT NULL DEFAULT 0,
  created_at TIMESTAMPTZ DEFAULT NOW(),
  updated_at TIMESTAMPTZ DEFAULT NOW(),
  published_at TIMESTAMPTZ,
  archived_at TIMESTAMPTZ
);

-- Seed Gift Card Product
INSERT INTO shop_products (
  sku,
  name,
  description,
  long_description,
  product_type,
  category,
  tags,
  base_price_usd,
  min_price_usd,
  allow_custom_amount,
  inventory_type,
  status,
  featured,
  sort_order,
  requires_shipping,
  published_at
) VALUES (
  'GIFTCARD-LILITH',
  'lilith Gift Card',
  'Store credit redeemable for subscriptions and merchandise.',
  'Purchase a lilith gift card and support platform development.',
  'gift_card',
  'Gift Cards',
  'gift-card,digital,store-credit',
  25.00,
  25.00,
  TRUE,
  'unlimited',
  'available',
  TRUE,
  0,
  FALSE,
  NOW()
) ON CONFLICT (sku) DO NOTHING;

-- =============================================================================
-- 4. Translations Table (for i18n tests)
-- =============================================================================

CREATE TABLE IF NOT EXISTS translations (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  locale VARCHAR(10) NOT NULL,
  namespace VARCHAR(100) NOT NULL,
  "keyPath" VARCHAR(255) NOT NULL,
  "translatedText" TEXT NOT NULL,
  "sourceHash" VARCHAR(64),
  provider VARCHAR(20) DEFAULT 'static',
  "qualityScore" FLOAT,
  "humanReviewed" BOOLEAN DEFAULT FALSE,
  "entityType" VARCHAR(50),
  "entityId" VARCHAR(100),
  "entityField" VARCHAR(50),
  "createdAt" TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
  "updatedAt" TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
  UNIQUE(locale, namespace, "keyPath")
);

-- Seed basic English translations
INSERT INTO translations (locale, namespace, "keyPath", "translatedText", provider) VALUES
  ('en', 'common', 'welcome', 'Welcome to lilith', 'static'),
  ('en', 'common', 'tagline', 'The ethical adult platform', 'static'),
  ('en', 'nav', 'home', 'Home', 'static'),
  ('en', 'nav', 'shop', 'Shop', 'static'),
  ('en', 'nav', 'providers', 'For Providers', 'static'),
  ('en', 'nav', 'clients', 'For Clients', 'static')
ON CONFLICT (locale, namespace, "keyPath") DO NOTHING;

-- =============================================================================
-- 5. Vote Economy Tables
-- =============================================================================

CREATE TABLE IF NOT EXISTS user_vote_balances (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  "userId" UUID NOT NULL UNIQUE,
  "totalVotesEarned" INTEGER NOT NULL DEFAULT 0,
  "votesSpent" INTEGER NOT NULL DEFAULT 0,
  "votesAvailable" INTEGER NOT NULL DEFAULT 0,
  "totalAmountSpent" DECIMAL(10,2) NOT NULL DEFAULT 0,
  "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
  "updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS vote_transactions (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  "userId" UUID NOT NULL,
  type vote_transaction_type NOT NULL,
  amount INTEGER NOT NULL,
  "balanceAfter" INTEGER NOT NULL,
  "relatedEntityId" UUID,
  "relatedEntityType" VARCHAR(50),
  metadata JSONB,
  "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW()
);

-- =============================================================================
-- Done
-- =============================================================================
