-- ToDoWrite Models Schema
-- Generated on: 2025-11-18T21:16:05.919141
-- Total tables: 33 (12 model tables + 21 association tables)
-- Database support: SQLite3 (this schema) + PostgreSQL
-- Note: All id fields are PRIMARY KEY, NOT NULL, and autoincrementing
--   - SQLite: INTEGER PRIMARY KEY AUTOINCREMENT
--   - PostgreSQL: id SERIAL PRIMARY KEY (recommended)
--     Generates: integer NOT NULL PRIMARY KEY DEFAULT nextval('table_name_id_seq'::regclass)
--     Modern alternative: id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY
--     Strict alternative: id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY
--
-- Foreign Key Constraints Pattern:
-- All association tables follow these conventions:
--   Table names: model1_model2 (alphabetical order, singular)
--   Column names: model_name_id (referencing model's id field)
--   Foreign keys: model_name_id INTEGER REFERENCES model_name(id)
--   Example: goals_phases (goal_id INTEGER REFERENCES goals(id), phase_id INTEGER REFERENCES phases(id))
-- This ensures referential integrity across all relationships in the ToDoWrite hierarchy.

CREATE TABLE acceptance_criteria (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    title VARCHAR NOT NULL,
    description TEXT,
    status VARCHAR NOT NULL,
    progress INTEGER,
    started_date VARCHAR,
    completion_date VARCHAR,
    owner VARCHAR,
    severity VARCHAR,
    work_type VARCHAR,
    assignee VARCHAR,
    extra_data TEXT,
    created_at VARCHAR NOT NULL,
    updated_at VARCHAR NOT NULL
);

CREATE TABLE acceptance_criteria_interface_contracts (
    acceptance_criteria_id INTEGER REFERENCES acceptance_criteria(id),
    interface_contract_id INTEGER REFERENCES interface_contracts(id)
);

CREATE TABLE acceptance_criteria_labels (
    acceptance_criteria_id INTEGER REFERENCES acceptance_criteria(id),
    label_id INTEGER REFERENCES labels(id)
);

CREATE TABLE commands (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    title VARCHAR NOT NULL,
    description TEXT,
    status VARCHAR NOT NULL,
    progress INTEGER,
    started_date VARCHAR,
    completion_date VARCHAR,
    owner VARCHAR,
    severity VARCHAR,
    work_type VARCHAR,
    assignee VARCHAR,
    acceptance_criteria_id INTEGER REFERENCES acceptance_criteria(id),
    cmd TEXT,
    cmd_params TEXT,
    runtime_env TEXT,
    output TEXT,
    artifacts TEXT,
    created_at VARCHAR NOT NULL,
    updated_at VARCHAR NOT NULL
);

CREATE TABLE commands_labels (
    command_id INTEGER REFERENCES commands(id),
    label_id INTEGER REFERENCES labels(id)
);

CREATE TABLE concepts (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    title VARCHAR NOT NULL,
    description TEXT,
    status VARCHAR NOT NULL,
    progress INTEGER,
    started_date VARCHAR,
    completion_date VARCHAR,
    owner VARCHAR,
    severity VARCHAR,
    work_type VARCHAR,
    assignee VARCHAR,
    extra_data TEXT,
    created_at VARCHAR NOT NULL,
    updated_at VARCHAR NOT NULL
);

CREATE TABLE concepts_contexts (
    concept_id INTEGER REFERENCES concepts(id),
    context_id INTEGER REFERENCES contexts(id)
);

CREATE TABLE concepts_labels (
    concept_id INTEGER REFERENCES concepts(id),
    label_id INTEGER REFERENCES labels(id)
);

CREATE TABLE constraints (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    title VARCHAR NOT NULL,
    description TEXT,
    status VARCHAR NOT NULL,
    progress INTEGER,
    started_date VARCHAR,
    completion_date VARCHAR,
    owner VARCHAR,
    severity VARCHAR,
    work_type VARCHAR,
    assignee VARCHAR,
    extra_data TEXT,
    created_at VARCHAR NOT NULL,
    updated_at VARCHAR NOT NULL
);

CREATE TABLE constraints_goals (
    constraint_id INTEGER REFERENCES constraints(id),
    goal_id INTEGER REFERENCES goals(id)
);

CREATE TABLE constraints_labels (
    constraint_id INTEGER REFERENCES constraints(id),
    label_id INTEGER REFERENCES labels(id)
);

CREATE TABLE constraints_requirements (
    constraint_id INTEGER REFERENCES constraints(id),
    requirement_id INTEGER REFERENCES requirements(id)
);

CREATE TABLE contexts (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    title VARCHAR NOT NULL,
    description TEXT,
    status VARCHAR NOT NULL,
    progress INTEGER,
    started_date VARCHAR,
    completion_date VARCHAR,
    owner VARCHAR,
    severity VARCHAR,
    work_type VARCHAR,
    assignee VARCHAR,
    extra_data TEXT,
    created_at VARCHAR NOT NULL,
    updated_at VARCHAR NOT NULL
);

CREATE TABLE contexts_labels (
    context_id INTEGER REFERENCES contexts(id),
    label_id INTEGER REFERENCES labels(id)
);

CREATE TABLE goals (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    title VARCHAR NOT NULL,
    description TEXT,
    status VARCHAR NOT NULL,
    progress INTEGER,
    started_date VARCHAR,
    completion_date VARCHAR,
    owner VARCHAR,
    severity VARCHAR,
    work_type VARCHAR,
    assignee VARCHAR,
    extra_data TEXT,
    created_at VARCHAR NOT NULL,
    updated_at VARCHAR NOT NULL
);

CREATE TABLE goals_concepts (
    goal_id INTEGER REFERENCES goals(id),
    concept_id INTEGER REFERENCES concepts(id)
);

CREATE TABLE goals_contexts (
    goal_id INTEGER REFERENCES goals(id),
    context_id INTEGER REFERENCES contexts(id)
);

CREATE TABLE goals_labels (
    goal_id INTEGER REFERENCES goals(id),
    label_id INTEGER REFERENCES labels(id)
);

CREATE TABLE goals_phases (
    goal_id INTEGER REFERENCES goals(id),
    phase_id INTEGER REFERENCES phases(id)
);

CREATE TABLE goals_tasks (
    goal_id INTEGER REFERENCES goals(id),
    task_id INTEGER REFERENCES tasks(id)
);

CREATE TABLE interface_contracts (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    title VARCHAR NOT NULL,
    description TEXT,
    status VARCHAR NOT NULL,
    progress INTEGER,
    started_date VARCHAR,
    completion_date VARCHAR,
    owner VARCHAR,
    severity VARCHAR,
    work_type VARCHAR,
    assignee VARCHAR,
    extra_data TEXT,
    created_at VARCHAR NOT NULL,
    updated_at VARCHAR NOT NULL
);

CREATE TABLE interface_contracts_labels (
    interface_contract_id INTEGER REFERENCES interface_contracts(id),
    label_id INTEGER REFERENCES labels(id)
);

CREATE TABLE interface_contracts_phases (
    interface_contract_id INTEGER REFERENCES interface_contracts(id),
    phase_id INTEGER REFERENCES phases(id)
);

CREATE TABLE labels (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name VARCHAR NOT NULL UNIQUE,
    created_at VARCHAR NOT NULL,
    updated_at VARCHAR NOT NULL
);

CREATE TABLE phases (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    title VARCHAR NOT NULL,
    description TEXT,
    status VARCHAR NOT NULL,
    progress INTEGER,
    started_date VARCHAR,
    completion_date VARCHAR,
    owner VARCHAR,
    severity VARCHAR,
    work_type VARCHAR,
    assignee VARCHAR,
    extra_data TEXT,
    created_at VARCHAR NOT NULL,
    updated_at VARCHAR NOT NULL
);

CREATE TABLE phases_labels (
    phase_id INTEGER REFERENCES phases(id),
    label_id INTEGER REFERENCES labels(id)
);

CREATE TABLE phases_steps (
    phase_id INTEGER REFERENCES phases(id),
    step_id INTEGER REFERENCES steps(id)
);

CREATE TABLE requirements (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    title VARCHAR NOT NULL,
    description TEXT,
    status VARCHAR NOT NULL,
    progress INTEGER,
    started_date VARCHAR,
    completion_date VARCHAR,
    owner VARCHAR,
    severity VARCHAR,
    work_type VARCHAR,
    assignee VARCHAR,
    extra_data TEXT,
    created_at VARCHAR NOT NULL,
    updated_at VARCHAR NOT NULL
);

CREATE TABLE requirements_acceptance_criteria (
    requirements_id INTEGER REFERENCES requirements(id),
    acceptance_criteria_id INTEGER REFERENCES acceptance_criteria(id)
);

CREATE TABLE requirements_concepts (
    requirement_id INTEGER REFERENCES requirements(id),
    concept_id INTEGER REFERENCES concepts(id)
);

CREATE TABLE requirements_contexts (
    requirement_id INTEGER REFERENCES requirements(id),
    context_id INTEGER REFERENCES contexts(id)
);

CREATE TABLE requirements_labels (
    requirement_id INTEGER REFERENCES requirements(id),
    label_id INTEGER REFERENCES labels(id)
);

CREATE TABLE steps (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    title VARCHAR NOT NULL,
    description TEXT,
    status VARCHAR NOT NULL,
    progress INTEGER,
    started_date VARCHAR,
    completion_date VARCHAR,
    owner VARCHAR,
    severity VARCHAR,
    work_type VARCHAR,
    assignee VARCHAR,
    extra_data TEXT,
    created_at VARCHAR NOT NULL,
    updated_at VARCHAR NOT NULL
);

CREATE TABLE steps_labels (
    step_id INTEGER REFERENCES steps(id),
    label_id INTEGER REFERENCES labels(id)
);

CREATE TABLE steps_tasks (
    step_id INTEGER REFERENCES steps(id),
    task_id INTEGER REFERENCES tasks(id)
);

CREATE TABLE sub_tasks (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    title VARCHAR NOT NULL,
    description TEXT,
    status VARCHAR NOT NULL,
    progress INTEGER,
    started_date VARCHAR,
    completion_date VARCHAR,
    owner VARCHAR,
    severity VARCHAR,
    work_type VARCHAR,
    assignee VARCHAR,
    extra_data TEXT,
    created_at VARCHAR NOT NULL,
    updated_at VARCHAR NOT NULL
);

CREATE TABLE sub_tasks_commands (
    sub_task_id INTEGER REFERENCES sub_tasks(id),
    command_id INTEGER REFERENCES commands(id)
);

CREATE TABLE sub_tasks_labels (
    sub_task_id INTEGER REFERENCES sub_tasks(id),
    label_id INTEGER REFERENCES labels(id)
);

CREATE TABLE tasks (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    title VARCHAR NOT NULL,
    description TEXT,
    status VARCHAR NOT NULL,
    progress INTEGER,
    started_date VARCHAR,
    completion_date VARCHAR,
    owner VARCHAR,
    severity VARCHAR,
    work_type VARCHAR,
    assignee VARCHAR,
    extra_data TEXT,
    created_at VARCHAR NOT NULL,
    updated_at VARCHAR NOT NULL
);

CREATE TABLE tasks_labels (
    task_id INTEGER REFERENCES tasks(id),
    label_id INTEGER REFERENCES labels(id)
);

CREATE TABLE tasks_sub_tasks (
    task_id INTEGER REFERENCES tasks(id),
    sub_task_id INTEGER REFERENCES sub_tasks(id)
);
