-- schema.sql

-- Schema for pit in python / SQLite mode
-- v1

--------------------------------
-- Project Table with trigger --
--------------------------------
CREATE TABLE project (
    id              integer primary key,
    username        text, -- User the project belongs to.
    name            text unique, -- Project name.
    status          text, -- Project status.
    created_at      date, -- When the project was created?
    updated_at      date  -- When the project was last updated?
);

CREATE TRIGGER project_insert_created_at AFTER INSERT ON project
BEGIN
    UPDATE project SET created_at = strftime('%Y-%m-%d %H:%M:%S.%s', 'now', 'localtime') WHERE id = new.id;
END;

CREATE TRIGGER project_update_updated_at AFTER UPDATE ON project
BEGIN
    UPDATE project SET updated_at = strftime('%Y-%m-%d %H:%M:%S.%s', 'now', 'localtime') WHERE id = new.id;
END;
--------------------------------

--------------------------------
--  Task Table with trigger   --
--------------------------------
CREATE TABLE task (
    id              integer primary key, -- TODO When delete, counter go down ==> problem?? Use autoincrement?
    project_id      integer,  -- Which project the task belongs to?
    username        text, -- User the task belongs to.
    name            text, -- Task name.
    status          text, -- Task status.
    priority        text, -- Task priority.
    date            date, -- Generic date/time, ex: task deadline.
    time            date, -- Generic time, ex: time spent on the task.
    created_at      date, -- When the task was created?
    updated_at      date  -- When the task was last updated?
);

CREATE TRIGGER task_insert_created_at AFTER INSERT ON task
BEGIN
    UPDATE task SET created_at = strftime('%Y-%m-%d %H:%M:%S.%s', 'now', 'localtime') WHERE id = new.id;
END;

CREATE TRIGGER task_update_updated_at AFTER UPDATE ON task
BEGIN
    UPDATE task SET updated_at = strftime('%Y-%m-%d %H:%M:%S.%s', 'now', 'localtime') WHERE id = new.id;
END;
--------------------------------

--------------------------------
--  Note Table with trigger   --
--------------------------------
CREATE TABLE note (
    id         integer primary key,
    project_id integer,  -- Project the note belongs to (0 if belongs to task).
    task_id    integer,  -- Task the note belongs to (0 if belongs to project).
    username   text, -- User who created the note.
    message    text, -- The body of the note.
    created_at date, -- When the note was created?
    updated_at date  -- When the note was last updated?
);

CREATE TRIGGER note_insert_created_at AFTER INSERT ON note
BEGIN
    UPDATE note SET created_at = strftime('%Y-%m-%d %H:%M:%S.%s', 'now', 'localtime') WHERE id = new.id;
END;

CREATE TRIGGER note_update_updated_at AFTER UPDATE ON note
BEGIN
    UPDATE note SET updated_at = strftime('%Y-%m-%d %H:%M:%S.%s', 'now', 'localtime') WHERE id = new.id;
END;
--------------------------------

--------------------------------
-- Action Table with trigger  --
--------------------------------
CREATE TABLE action (
    id         integer primary key,
    project_id integer,  -- Project id (always set).
    task_id    integer,  -- Task id (set for task or note related actions).
    note_id    integer,  -- Note id (set for note related actions only).
    username   text, -- Who added the log message?
    action     text, -- What type of action ? (updated, created, etc.)
    message    text, -- Log message.
    created_at date  -- When log message was added?
);

CREATE TRIGGER action_insert_created_at AFTER INSERT ON action
BEGIN
    UPDATE action SET created_at = strftime('%Y-%m-%d %H:%M:%S.%s', 'now', 'localtime') WHERE id = new.id;
END;
--------------------------------

--------------------------------
-- Current table with trigger --
--------------------------------
CREATE TABLE current (
    project_id integer,
    task_id    integer,
    note_id    integer
);

INSERT INTO current VALUES (0,0,0);

-- Trigger after insert for current values
CREATE TRIGGER current_project AFTER INSERT ON project
BEGIN
    UPDATE current SET project_id = new.id;
END;

CREATE TRIGGER current_task AFTER INSERT ON task
BEGIN
    UPDATE current SET task_id = new.id;
END;

CREATE TRIGGER current_note AFTER INSERT ON note
BEGIN
    UPDATE current SET note_id = new.id;
END;

-- Trigger after update for current values
CREATE TRIGGER update_project AFTER UPDATE ON project
BEGIN
    UPDATE current SET project_id = new.id;
END;

CREATE TRIGGER update_task AFTER UPDATE ON task
BEGIN
    UPDATE current SET task_id = new.id;
END;

CREATE TRIGGER update_note AFTER UPDATE ON note
BEGIN
    UPDATE current SET note_id = new.id;
END;

-- Trigger after delete for current values
CREATE TRIGGER delete_project AFTER DELETE ON project
BEGIN
    UPDATE current SET project_id = 0;
END;

CREATE TRIGGER delete_task AFTER DELETE ON task
BEGIN
    UPDATE current SET task_id = 0;
END;

CREATE TRIGGER delete_note AFTER DELETE ON note
BEGIN
    UPDATE current SET note_id = 0;
END;
--------------------------------

