create table providers (
    id              text not null primary key,
    team_id         text not null,
    name            text not null,
    key             text,
    url             text,
    _date_created    numeric not null default current_timestamp,
    _date_updated    numeric,
    _version        text not null
);

create table models (
    id              text not null primary key,
    team_id         text not null,
    provider_id     text not null,
    name            text not null default '',
    modelname       text not null default '',
    configuration   text not null default '{}',
    _date_created   numeric not null default current_timestamp,
    _date_updated   numeric,
    _version        text not null
);

create table settings (
    id               text not null primary key,
    default_lang     text not null default 'en',
    register_enabled numeric not null default 1,
    password_enabled numeric not null default 1,
    _date_created    numeric not null default current_timestamp,
    _date_updated    numeric,
    _version         text not null
);

create table oidc (
    id              text not null primary key,
    label           text not null,
    domain          text not null,
    client_id       text not null,
    client_secret   text not null,
    wellknown_url   text not null,
    _date_created   numeric not null default current_timestamp,
    _date_updated   numeric,
    _version        text not null
);

create table users (
    id              text not null primary key,
    firstname       text not null default '',
    lastname        text not null default '',
    password        text not null default '',
    email           text not null unique,
    picture         text not null default '',
    enabled         numeric default true,
    theme           text default 'auto',
    lang            text default 'en',
    _date_created   numeric not null default current_timestamp,
    _date_updated   numeric,
    _version        text not null
);

create table user_addresses (
    id              text not null primary key,
    user_id         text not null,
    type            text not null,
    address         text not null,
    _date_created    numeric not null default current_timestamp,
    _date_updated    numeric,
    _version        text not null
);

create table teams (
    id               text not null primary key,
    label            text not null,
    default_model_id text,
    _date_created    numeric not null default current_timestamp,
    _date_updated    numeric ,
    _version         text not null
);

create table user_teams (
    id              text not null primary key,
    user_id         text not null,
    team_id         text not null,
    administrator   bool default 0,
    _date_created   numeric not null default current_timestamp,
    _date_updated   numeric,
    _version        text not null
);

create table channels (
    id              text not null primary key,
    team_id         text not null,
    name            text not null,
    type            text not null,
    enabled         numeric not null default 1,
    configuration   text not null default '{}',
    _date_created   numeric not null default current_timestamp,
    _date_updated   numeric,
    _version        text not null
);

create table tools (
    id              text not null primary key,
    team_id         text not null,
    name            text not null,
    type            text not null,
    enabled         numeric not null default 1,
    configuration   text not null default '{}',
    _date_created   numeric not null default current_timestamp,
    _date_updated   numeric,
    _version        text not null
);

create table tasks (
    id              text not null primary key,
    team_id        text not null,
    model_id        text not null,
    label           text not null,
    prompt          text not null,
    cron            text not null,
    status          text not null,
    next_datetime   numeric,
    _date_created   numeric not null default current_timestamp,
    _date_updated   numeric,
    _version        text not null
);

create table history (
    id              text not null primary key,
    task_id         text not null,
    start_datetime  numeric,
    end_datetime    numeric,
    prompt          text not null,
    log             text not null,
    response        text not null,
    _date_created   numeric not null default current_timestamp,
    _date_updated   numeric,
    _version        text not null
);

create table files (
    id              text not null primary key,
    name            text not null default '',
    content_type    text not null default '',
    storage_path    text not null default '',
    storage_filename text not null default '',
    content         blob,
    _date_created   numeric not null default current_timestamp,
    _date_updated   numeric ,
    _version        text not null
);

create table share (
    id              text not null primary key,
    object_id       text not null,
    object_type     text not null,
    team_id         text not null,
    enabled         numeric not null default 0,
    _date_created   numeric not null default current_timestamp,
    _date_updated   numeric,
    _version        text not null
);
