CREATE TABLE folders (
    id UUID PRIMARY KEY NOT NULL,
    title VARCHAR NOT NULL,
    sort_order INTEGER NOT NULL UNIQUE,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE projects (
    id UUID PRIMARY KEY NOT NULL,
    title VARCHAR NOT NULL,
    color VARCHAR(7) NOT NULL, -- hex code
    folder_id UUID,
    sort_order INTEGER NOT NULL UNIQUE,
    status TEXT CHECK(status IN ('active', 'done', 'backlog')) DEFAULT 'active',
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    completed_at TIMESTAMP,
    FOREIGN KEY(folder_id) REFERENCES folders(id)
);

ALTER TABLE tasks RENAME TO tasks_old;

-- Add up migration script here
CREATE TABLE tasks (
    id UUID PRIMARY KEY NOT NULL,
    title VARCHAR NOT NULL,
    description TEXT,
    status TEXT CHECK(status IN ('todo', 'done', 'backlog')) DEFAULT 'todo',
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    completed_at TIMESTAMP,
    project_id UUID,
    FOREIGN KEY(project_id) REFERENCES projects(id)
);

INSERT INTO tasks SELECT *, NULL as project_id FROM tasks_old;

DROP TABLE tasks_old;
