-- Migration: 003_create_urt_enums.sql
-- Purpose: Create enum types and lookup tables for URT taxonomy

-- Valence enum
DO $$ BEGIN
    CREATE TYPE valence_type AS ENUM ('V+', 'V-', 'V0', 'V±');
EXCEPTION
    WHEN duplicate_object THEN NULL;
END $$;

-- Intensity enum
DO $$ BEGIN
    CREATE TYPE intensity_type AS ENUM ('I1', 'I2', 'I3');
EXCEPTION
    WHEN duplicate_object THEN NULL;
END $$;

-- Specificity enum
DO $$ BEGIN
    CREATE TYPE specificity_type AS ENUM ('S1', 'S2', 'S3');
EXCEPTION
    WHEN duplicate_object THEN NULL;
END $$;

-- Actionability enum
DO $$ BEGIN
    CREATE TYPE actionability_type AS ENUM ('A1', 'A2', 'A3');
EXCEPTION
    WHEN duplicate_object THEN NULL;
END $$;

-- Temporal enum
DO $$ BEGIN
    CREATE TYPE temporal_type AS ENUM ('TC', 'TR', 'TH', 'TF');
EXCEPTION
    WHEN duplicate_object THEN NULL;
END $$;

-- Evidence enum
DO $$ BEGIN
    CREATE TYPE evidence_type AS ENUM ('ES', 'EI', 'EC');
EXCEPTION
    WHEN duplicate_object THEN NULL;
END $$;

-- Comparative enum
DO $$ BEGIN
    CREATE TYPE comparative_type AS ENUM ('CR-N', 'CR-B', 'CR-W', 'CR-S');
EXCEPTION
    WHEN duplicate_object THEN NULL;
END $$;

-- URT Domain lookup table
CREATE TABLE IF NOT EXISTS urt_domains (
    code CHAR(1) PRIMARY KEY,
    name VARCHAR(50) NOT NULL,
    description TEXT
);

INSERT INTO urt_domains (code, name, description) VALUES
    ('O', 'Offering', 'Product/service quality, features, variety'),
    ('P', 'Price', 'Value, pricing, promotions, payment'),
    ('J', 'Journey', 'Timing, process, convenience, accessibility'),
    ('E', 'Environment', 'Physical space, ambiance, cleanliness, digital UX'),
    ('A', 'Attitude', 'Staff behavior, helpfulness, professionalism'),
    ('V', 'Voice', 'Brand, communication, marketing, transparency'),
    ('R', 'Relationship', 'Loyalty, trust, consistency, personalization')
ON CONFLICT (code) DO NOTHING;

-- URT Tier-2 categories lookup table
CREATE TABLE IF NOT EXISTS urt_categories (
    code VARCHAR(5) PRIMARY KEY,
    domain_code CHAR(1) NOT NULL REFERENCES urt_domains(code),
    name VARCHAR(100) NOT NULL,
    description TEXT
);

-- Insert standard Tier-2 categories
INSERT INTO urt_categories (code, domain_code, name) VALUES
    ('O1', 'O', 'Core Product/Service'),
    ('O2', 'O', 'Product Features'),
    ('O3', 'O', 'Variety & Selection'),
    ('O4', 'O', 'Customization'),
    ('P1', 'P', 'Value Perception'),
    ('P2', 'P', 'Pricing Structure'),
    ('P3', 'P', 'Promotions & Deals'),
    ('P4', 'P', 'Payment Process'),
    ('J1', 'J', 'Wait Times'),
    ('J2', 'J', 'Booking & Reservations'),
    ('J3', 'J', 'Navigation & Convenience'),
    ('J4', 'J', 'Accessibility'),
    ('E1', 'E', 'Physical Environment'),
    ('E2', 'E', 'Ambiance & Atmosphere'),
    ('E3', 'E', 'Cleanliness'),
    ('E4', 'E', 'Digital Experience'),
    ('A1', 'A', 'Friendliness'),
    ('A2', 'A', 'Helpfulness'),
    ('A3', 'A', 'Professionalism'),
    ('A4', 'A', 'Knowledge & Expertise'),
    ('V1', 'V', 'Brand Identity'),
    ('V2', 'V', 'Communication'),
    ('V3', 'V', 'Marketing'),
    ('V4', 'V', 'Transparency'),
    ('R1', 'R', 'Loyalty'),
    ('R2', 'R', 'Trust'),
    ('R3', 'R', 'Consistency'),
    ('R4', 'R', 'Personalization')
ON CONFLICT (code) DO NOTHING;

COMMENT ON TABLE urt_domains IS 'URT v5.1 top-level domains';
COMMENT ON TABLE urt_categories IS 'URT v5.1 Tier-2 categories';
