-- Initial schema for Mangalord.
-- Designed for future extensions: tags, lists, fulltext/fuzzy search,
-- OCR-derived metadata. New concepts get new tables joined here; do
-- not jam them onto existing rows.

CREATE EXTENSION IF NOT EXISTS "pgcrypto";

CREATE TABLE users (
    id          uuid PRIMARY KEY DEFAULT gen_random_uuid(),
    username    text NOT NULL UNIQUE,
    created_at  timestamptz NOT NULL DEFAULT now()
);

CREATE TABLE mangas (
    id                uuid PRIMARY KEY DEFAULT gen_random_uuid(),
    title             text NOT NULL,
    author            text,
    description       text,
    cover_image_path  text,
    created_at        timestamptz NOT NULL DEFAULT now(),
    updated_at        timestamptz NOT NULL DEFAULT now()
);

CREATE INDEX mangas_created_at_idx ON mangas (created_at DESC);
CREATE INDEX mangas_title_lower_idx ON mangas (lower(title));

CREATE TABLE chapters (
    id           uuid PRIMARY KEY DEFAULT gen_random_uuid(),
    manga_id     uuid NOT NULL REFERENCES mangas(id) ON DELETE CASCADE,
    number       integer NOT NULL,
    title        text,
    page_count   integer NOT NULL DEFAULT 0,
    created_at   timestamptz NOT NULL DEFAULT now(),
    UNIQUE (manga_id, number)
);

CREATE INDEX chapters_manga_idx ON chapters (manga_id, number);

CREATE TABLE bookmarks (
    id           uuid PRIMARY KEY DEFAULT gen_random_uuid(),
    user_id      uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
    manga_id     uuid NOT NULL REFERENCES mangas(id) ON DELETE CASCADE,
    chapter_id   uuid REFERENCES chapters(id) ON DELETE SET NULL,
    page         integer,
    created_at   timestamptz NOT NULL DEFAULT now(),
    UNIQUE (user_id, manga_id, chapter_id)
);

CREATE INDEX bookmarks_user_idx ON bookmarks (user_id, created_at DESC);
