-- Version 1 is the initial structure.
CREATE TABLE [Version] (
    [id] INTEGER PRIMARY KEY,
    [version] INTEGER NOT NULL UNIQUE,
    [datetime] DATETIME
);

CREATE TABLE [User] (
    [id] INTEGER PRIMARY KEY,
    [email] TEXT NOT NULL,
    [name] TEXT,
    [default_servings] INTEGER DEFAULT 4,

    [password] TEXT NOT NULL, -- argon2(password_plain, salt).

    [creation_datetime] DATETIME NOT NULL, -- Updated when the validation email is sent.
    [validation_token] TEXT, -- If not null then the user has not validated his account yet.

    [is_admin] INTEGER NOT NULL DEFAULT FALSE
);

CREATE UNIQUE INDEX [User_email_index] ON [User] ([email]);

CREATE TABLE [UserLoginToken] (
    [id] INTEGER PRIMARY KEY,
    [user_id] INTEGER NOT NULL,
    [last_login_datetime] DATETIME,
    [token] TEXT NOT NULL, --  24 alphanumeric character token. Can be stored in a cookie to be able to authenticate without a password.

    [ip] TEXT, -- Can be ipv4 or ipv6
    [user_agent] TEXT,

    FOREIGN KEY([user_id]) REFERENCES [User]([id]) ON DELETE CASCADE
);

CREATE INDEX [UserLoginToken_token_index] ON [UserLoginToken] ([token]);

CREATE TABLE [Recipe] (
    [id] INTEGER PRIMARY KEY,
    [user_id] INTEGER, -- Can be null if a user is deleted.
    [title] TEXT NOT NULL,
    [estimate_time] INTEGER,
    [description] TEXT,
    [servings] INTEGER DEFAULT 4,

    FOREIGN KEY([user_id]) REFERENCES [User]([id]) ON DELETE SET NULL
);

CREATE TABLE [RecipeTag] (
    [id] INTEGER PRIMARY KEY,

    [recipe_id] INTEGER NOT NULL,
    [tag_id] INTEGER NO NULL,

    FOREIGN KEY([recipe_id]) REFERENCES [Recipe]([id]) ON DELETE CASCADE,
    FOREIGN KEY([tag_id]) REFERENCES [Tag]([id]) ON DELETE CASCADE
);

CREATE TABLE [Tag] (
    [id] INTEGER PRIMARY KEY,
    [recipe_tag_id] INTEGER,
    [name] TEXT NOT NULL,

    FOREIGN KEY([recipe_tag_id]) REFERENCES [RecipeTag]([id]) ON DELETE SET NULL
);

CREATE UNIQUE INDEX [Tag_name_index] ON [Tag] ([name]);

CREATE TABLE [Quantity] (
    [id] INTEGER PRIMARY KEY,
    [value] REAL,
    [unit] TEXT
);

CREATE TABLE [Ingredient] (
    [id] INTEGER PRIMARY KEY,
    [name] TEXT NOT NULL,
    [quantity_id] INTEGER,
    [input_step_id] INTEGER NOT NULL,

    FOREIGN KEY([quantity_id]) REFERENCES Quantity([id]) ON DELETE CASCADE,
    FOREIGN KEY([input_step_id]) REFERENCES Step([id]) ON DELETE CASCADE
);

CREATE TABLE [Group] (
    [id] INTEGER PRIMARY KEY,
    [order] INTEGER NOT NULL DEFAULT 0,
    [recipe_id] INTEGER,
    name TEXT,

    FOREIGN KEY([recipe_id]) REFERENCES [Recipe]([id]) ON DELETE CASCADE
);

CREATE INDEX [Group_order_index] ON [Group] ([order]);

CREATE TABLE [Step] (
    [id] INTEGER PRIMARY KEY,
    [order] INTEGER NOT NULL DEFAULT 0,
    [action] TEXT NOT NULL,
    [group_id] INTEGER NOT NULL,

    FOREIGN KEY(group_id) REFERENCES [Group](id) ON DELETE CASCADE
);

CREATE INDEX [Step_order_index] ON [Group] ([order]);

CREATE TABLE [IntermediateSubstance] (
    [id] INTEGER PRIMARY KEY,
    [name] TEXT NOT NULL,
    [quantity_id] INTEGER,
    [output_step_id] INTEGER NOT NULL,
    [input_step_id] INTEGER NOT NULL,

    FOREIGN KEY([quantity_id]) REFERENCES [Quantity]([id]) ON DELETE CASCADE,
    FOREIGN KEY([output_step_id]) REFERENCES [Step]([id]) ON DELETE CASCADE,
    FOREIGN KEY([input_step_id]) REFERENCES [Step]([id]) ON DELETE CASCADE
);
