CREATE TABLE IF NOT EXISTS project_board
(
    id           UUID PRIMARY KEY      DEFAULT gen_random_uuid(),
    project_uuid UUID         NOT NULL,
    name         VARCHAR(255) NOT NULL,
    description  TEXT,
    created_by   UUID         NOT NULL,
    created_at   TIMESTAMPTZ  NOT NULL DEFAULT NOW(),
    updated_at   TIMESTAMPTZ  NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_project_board_project ON project_board (project_uuid);

CREATE TABLE IF NOT EXISTS project_board_column
(
    id         UUID PRIMARY KEY      DEFAULT gen_random_uuid(),
    board_uuid UUID         NOT NULL REFERENCES project_board (id) ON DELETE CASCADE,
    name       VARCHAR(255) NOT NULL,
    position   INTEGER      NOT NULL DEFAULT 0,
    wip_limit  INTEGER,
    color      VARCHAR(20)
);

CREATE INDEX IF NOT EXISTS idx_project_board_column_board ON project_board_column (board_uuid);

CREATE TABLE IF NOT EXISTS project_board_card
(
    id          UUID PRIMARY KEY      DEFAULT gen_random_uuid(),
    column_uuid UUID         NOT NULL REFERENCES project_board_column (id) ON DELETE CASCADE,
    issue_id    BIGINT,
    project     UUID,
    title       VARCHAR(500) NOT NULL,
    description TEXT,
    position    INTEGER      NOT NULL DEFAULT 0,
    assignee_id UUID,
    due_date    TIMESTAMPTZ,
    priority    VARCHAR(10),
    created_by  UUID         NOT NULL,
    created_at  TIMESTAMPTZ  NOT NULL DEFAULT NOW(),
    updated_at  TIMESTAMPTZ  NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_project_board_card_column ON project_board_card (column_uuid);
CREATE INDEX IF NOT EXISTS idx_project_board_card_issue ON project_board_card (issue_id) WHERE issue_id IS NOT NULL;
