-- E-shop tables for MyClub
-- These tables support product catalog, shopping cart, orders, payments, and shipping

-- Product categories
CREATE TABLE IF NOT EXISTS eshop_product_categories (
    id SERIAL PRIMARY KEY,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    deleted_at TIMESTAMP WITH TIME ZONE,

    slug VARCHAR(190) UNIQUE NOT NULL,
    name VARCHAR(255) NOT NULL,
    parent_id INTEGER REFERENCES eshop_product_categories(id),
    display_order INTEGER DEFAULT 0,
    active BOOLEAN DEFAULT true
);

-- Products
CREATE TABLE IF NOT EXISTS eshop_products (
    id SERIAL PRIMARY KEY,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    deleted_at TIMESTAMP WITH TIME ZONE,

    slug VARCHAR(190) UNIQUE NOT NULL,
    name VARCHAR(255) NOT NULL,
    short_description TEXT,
    description_html TEXT,
    price_cents BIGINT NOT NULL,
    currency VARCHAR(10) DEFAULT 'CZK',
    vat_rate DECIMAL(5,4),
    active BOOLEAN DEFAULT true,
    stock_mode VARCHAR(20) DEFAULT 'finite',
    default_image_url VARCHAR(500),
    gallery_json TEXT,
    tags TEXT,
    metadata_json TEXT,
    category_id INTEGER REFERENCES eshop_product_categories(id)
);

-- Product variants
CREATE TABLE IF NOT EXISTS eshop_product_variants (
    id SERIAL PRIMARY KEY,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    deleted_at TIMESTAMP WITH TIME ZONE,

    product_id INTEGER NOT NULL REFERENCES eshop_products(id) ON DELETE CASCADE,
    sku VARCHAR(64),
    name VARCHAR(255),
    attributes_json TEXT,
    stock_qty INTEGER DEFAULT 0,
    barcode VARCHAR(128),
    image_url VARCHAR(500)
);

-- Shopping carts
CREATE TABLE IF NOT EXISTS eshop_carts (
    id SERIAL PRIMARY KEY,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    deleted_at TIMESTAMP WITH TIME ZONE,

    user_id INTEGER REFERENCES users(id),
    session_token VARCHAR(64),
    currency VARCHAR(10),
    completed BOOLEAN DEFAULT false
);

-- Cart items
CREATE TABLE IF NOT EXISTS eshop_cart_items (
    id SERIAL PRIMARY KEY,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    deleted_at TIMESTAMP WITH TIME ZONE,

    cart_id INTEGER NOT NULL REFERENCES eshop_carts(id) ON DELETE CASCADE,
    product_id INTEGER NOT NULL REFERENCES eshop_products(id),
    variant_id INTEGER REFERENCES eshop_product_variants(id),
    quantity INTEGER NOT NULL DEFAULT 1,
    unit_price_cents BIGINT NOT NULL,
    currency VARCHAR(10)
);

-- Orders
CREATE TABLE IF NOT EXISTS eshop_orders (
    id SERIAL PRIMARY KEY,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    deleted_at TIMESTAMP WITH TIME ZONE,

    order_number VARCHAR(32) UNIQUE NOT NULL,
    user_id INTEGER REFERENCES users(id),
    session_token VARCHAR(64),
    email VARCHAR(255),
    first_name VARCHAR(100),
    last_name VARCHAR(100),
    billing_address_json TEXT,
    shipping_address_json TEXT,
    status VARCHAR(32) DEFAULT 'new',
    total_amount_cents BIGINT NOT NULL,
    currency VARCHAR(10),
    shipping_method VARCHAR(32),
    shipping_price_cents BIGINT DEFAULT 0,
    shipping_data_json TEXT,
    metadata_json TEXT
);

-- Order items
CREATE TABLE IF NOT EXISTS eshop_order_items (
    id SERIAL PRIMARY KEY,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    deleted_at TIMESTAMP WITH TIME ZONE,

    order_id INTEGER NOT NULL REFERENCES eshop_orders(id) ON DELETE CASCADE,
    product_id INTEGER NOT NULL,
    variant_id INTEGER REFERENCES eshop_product_variants(id),
    name VARCHAR(255) NOT NULL,
    sku VARCHAR(64),
    quantity INTEGER NOT NULL DEFAULT 1,
    unit_price_cents BIGINT NOT NULL,
    currency VARCHAR(10),
    vat_rate DECIMAL(5,4)
);

-- Payments
CREATE TABLE IF NOT EXISTS eshop_payments (
    id SERIAL PRIMARY KEY,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    deleted_at TIMESTAMP WITH TIME ZONE,

    order_id INTEGER NOT NULL REFERENCES eshop_orders(id) ON DELETE CASCADE,
    provider VARCHAR(32),
    provider_payment_id VARCHAR(128),
    status VARCHAR(32) DEFAULT 'pending',
    amount_cents BIGINT NOT NULL,
    currency VARCHAR(10),
    raw_payload_json TEXT
);

-- Shipping labels
CREATE TABLE IF NOT EXISTS eshop_shipping_labels (
    id SERIAL PRIMARY KEY,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    deleted_at TIMESTAMP WITH TIME ZONE,

    order_id INTEGER NOT NULL REFERENCES eshop_orders(id) ON DELETE CASCADE,
    carrier VARCHAR(32),
    packeta_packet_id VARCHAR(64),
    tracking_number VARCHAR(64),
    label_url VARCHAR(500),
    status VARCHAR(64),
    history_json TEXT
);

-- E-shop settings
CREATE TABLE IF NOT EXISTS eshop_settings (
    id SERIAL PRIMARY KEY,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    deleted_at TIMESTAMP WITH TIME ZONE,

    default_currency VARCHAR(10) DEFAULT 'CZK',
    supported_currencies TEXT,
    default_country VARCHAR(2) DEFAULT 'CZ',
    shipping_options_json TEXT,
    terms_url VARCHAR(500),
    returns_policy_url VARCHAR(500),
    support_email VARCHAR(255),
    support_phone VARCHAR(64)
);

-- Indexes for performance
CREATE INDEX IF NOT EXISTS idx_eshop_products_active ON eshop_products(active) WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS idx_eshop_products_slug ON eshop_products(slug) WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS idx_eshop_products_category ON eshop_products(category_id) WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS idx_eshop_variants_product ON eshop_product_variants(product_id) WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS idx_eshop_variants_sku ON eshop_product_variants(sku) WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS idx_eshop_carts_user ON eshop_carts(user_id) WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS idx_eshop_carts_session ON eshop_carts(session_token) WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS idx_eshop_carts_completed ON eshop_carts(completed) WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS idx_eshop_cart_items_cart ON eshop_cart_items(cart_id) WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS idx_eshop_orders_user ON eshop_orders(user_id) WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS idx_eshop_orders_session ON eshop_orders(session_token) WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS idx_eshop_orders_status ON eshop_orders(status) WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS idx_eshop_orders_number ON eshop_orders(order_number) WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS idx_eshop_payments_order ON eshop_payments(order_id) WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS idx_eshop_payments_provider ON eshop_payments(provider, provider_payment_id) WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS idx_eshop_shipping_order ON eshop_shipping_labels(order_id) WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS idx_eshop_shipping_packet ON eshop_shipping_labels(packeta_packet_id) WHERE deleted_at IS NULL;
