-- name: InitializeSchema :exec
CREATE TABLE IF NOT EXISTS content (
    id TEXT NOT NULL,
    site_id TEXT NOT NULL,
    html_content TEXT NOT NULL,
    original_template TEXT,
    created_at BIGINT DEFAULT (EXTRACT(EPOCH FROM NOW())) NOT NULL,
    updated_at BIGINT DEFAULT (EXTRACT(EPOCH FROM NOW())) NOT NULL,
    last_edited_by TEXT DEFAULT 'system' NOT NULL,
    PRIMARY KEY (id, site_id)
);

-- name: InitializeVersionsTable :exec
CREATE TABLE IF NOT EXISTS content_versions (
    version_id SERIAL PRIMARY KEY,
    content_id TEXT NOT NULL,
    site_id TEXT NOT NULL,
    html_content TEXT NOT NULL,
    original_template TEXT,
    created_at BIGINT DEFAULT (EXTRACT(EPOCH FROM NOW())) NOT NULL,
    created_by TEXT DEFAULT 'system' NOT NULL
);

-- name: InitializeCollectionsTable :exec
CREATE TABLE IF NOT EXISTS collections (
    id TEXT NOT NULL,
    site_id TEXT NOT NULL,
    container_html TEXT NOT NULL,
    created_at BIGINT DEFAULT (EXTRACT(EPOCH FROM NOW())) NOT NULL,
    updated_at BIGINT DEFAULT (EXTRACT(EPOCH FROM NOW())) NOT NULL,
    last_edited_by TEXT DEFAULT 'system' NOT NULL,
    PRIMARY KEY (id, site_id)
);

-- name: InitializeCollectionTemplatesTable :exec
CREATE TABLE IF NOT EXISTS collection_templates (
    template_id SERIAL PRIMARY KEY,
    collection_id TEXT NOT NULL,
    site_id TEXT NOT NULL,
    name TEXT NOT NULL,
    html_template TEXT NOT NULL,
    is_default BOOLEAN DEFAULT FALSE NOT NULL,
    created_at BIGINT DEFAULT (EXTRACT(EPOCH FROM NOW())) NOT NULL,
    FOREIGN KEY (collection_id, site_id) REFERENCES collections(id, site_id) ON DELETE CASCADE
);

-- name: InitializeCollectionItemsTable :exec
CREATE TABLE IF NOT EXISTS collection_items (
    item_id TEXT NOT NULL,
    collection_id TEXT NOT NULL,
    site_id TEXT NOT NULL,
    template_id INTEGER NOT NULL,
    html_content TEXT NOT NULL,
    position INTEGER NOT NULL,
    created_at BIGINT DEFAULT (EXTRACT(EPOCH FROM NOW())) NOT NULL,
    updated_at BIGINT DEFAULT (EXTRACT(EPOCH FROM NOW())) NOT NULL,
    last_edited_by TEXT DEFAULT 'system' NOT NULL,
    PRIMARY KEY (item_id, collection_id, site_id),
    FOREIGN KEY (collection_id, site_id) REFERENCES collections(id, site_id) ON DELETE CASCADE,
    FOREIGN KEY (template_id) REFERENCES collection_templates(template_id) ON DELETE RESTRICT
);

-- name: InitializeCollectionItemVersionsTable :exec
CREATE TABLE IF NOT EXISTS collection_item_versions (
    version_id SERIAL PRIMARY KEY,
    item_id TEXT NOT NULL,
    collection_id TEXT NOT NULL,
    site_id TEXT NOT NULL,
    html_content TEXT NOT NULL,
    template_id INTEGER NOT NULL,
    position INTEGER NOT NULL,
    created_at BIGINT DEFAULT (EXTRACT(EPOCH FROM NOW())) NOT NULL,
    created_by TEXT DEFAULT 'system' NOT NULL,
    FOREIGN KEY (item_id, collection_id, site_id) REFERENCES collection_items(item_id, collection_id, site_id) ON DELETE CASCADE
);

-- name: CreateContentSiteIndex :exec
CREATE INDEX IF NOT EXISTS idx_content_site_id ON content(site_id);

-- name: CreateContentUpdatedAtIndex :exec
CREATE INDEX IF NOT EXISTS idx_content_updated_at ON content(updated_at);

-- name: CreateVersionsLookupIndex :exec
CREATE INDEX IF NOT EXISTS idx_content_versions_lookup ON content_versions(content_id, site_id, created_at DESC);

-- name: CreateCollectionsSiteIndex :exec
CREATE INDEX IF NOT EXISTS idx_collections_site_id ON collections(site_id);

-- name: CreateCollectionsUpdatedAtIndex :exec
CREATE INDEX IF NOT EXISTS idx_collections_updated_at ON collections(updated_at);

-- name: CreateCollectionTemplatesLookupIndex :exec
CREATE INDEX IF NOT EXISTS idx_collection_templates_lookup ON collection_templates(collection_id, site_id);

-- name: CreateCollectionTemplatesDefaultIndex :exec
CREATE INDEX IF NOT EXISTS idx_collection_templates_default ON collection_templates(collection_id, site_id, is_default DESC);

-- name: CreateCollectionItemsLookupIndex :exec
CREATE INDEX IF NOT EXISTS idx_collection_items_lookup ON collection_items(collection_id, site_id, position);

-- name: CreateCollectionItemsTemplateIndex :exec
CREATE INDEX IF NOT EXISTS idx_collection_items_template ON collection_items(template_id);

-- name: CreateCollectionItemVersionsLookupIndex :exec
CREATE INDEX IF NOT EXISTS idx_collection_item_versions_lookup ON collection_item_versions(item_id, collection_id, site_id, created_at DESC);

-- name: CreateCollectionTemplatesOneDefaultIndex :exec
CREATE UNIQUE INDEX IF NOT EXISTS idx_collection_templates_one_default 
ON collection_templates(collection_id, site_id) 
WHERE is_default = TRUE;

-- name: CreateUpdateFunction :exec
CREATE OR REPLACE FUNCTION update_content_timestamp()
RETURNS TRIGGER AS $$
BEGIN
    NEW.updated_at = EXTRACT(EPOCH FROM NOW());
    RETURN NEW;
END;
$$ LANGUAGE plpgsql;

-- name: CreateUpdateTrigger :exec
DROP TRIGGER IF EXISTS update_content_updated_at ON content;
CREATE TRIGGER update_content_updated_at
BEFORE UPDATE ON content
FOR EACH ROW
EXECUTE FUNCTION update_content_timestamp();

-- name: CreateCollectionsUpdateTrigger :exec
DROP TRIGGER IF EXISTS update_collections_updated_at ON collections;
CREATE TRIGGER update_collections_updated_at
BEFORE UPDATE ON collections
FOR EACH ROW
EXECUTE FUNCTION update_content_timestamp();

-- name: CreateCollectionItemsUpdateTrigger :exec
DROP TRIGGER IF EXISTS update_collection_items_updated_at ON collection_items;
CREATE TRIGGER update_collection_items_updated_at
BEFORE UPDATE ON collection_items
FOR EACH ROW
EXECUTE FUNCTION update_content_timestamp();