drop table if exists choices;
drop table if exists attachments;
drop table if exists votes;
drop table if exists roles;
drop table if exists users;

create table users (
    id INTEGER primary key autoincrement,
    email TEXT unique not null,
    password TEXT not null,
    name TEXT unique, 
    organization TEXT,
    is_admin INTEGER default 0 not null,
    key TEXT
);

create table roles (
    id INTEGER primary key autoincrement,
    name TEXT,
    system INTEGER default 0 not null 
);

create table votes (
    id INTEGER primary key autoincrement,
    title TEXT not null,
    description TEXT,
    category TEXT,
    date_begin INTEGER default CURRENT_TIMESTAMP not null,
    date_end INTEGER not null,
    is_transparent INTEGER default 1 not null, 
    is_public INTEGER default 1 not null,
    is_multiplechoice INTEGER default 1 not null,
    is_weighted INTEGER default 0 not null,
    is_open INTEGER default 0 not null,
    id_author INTEGER, -- :COMMENT:maethor:120528: not null ?
    id_role INTEGER default 1 not null,
    FOREIGN KEY(id_author) REFERENCES users(id)
    FOREIGN KEY(id_role) REFERENCES roles(id)
);

create table attachments (
    url TEXT not null,
    id_vote INTEGER not null,
    FOREIGN KEY(id_vote) REFERENCES vote(id),
    PRIMARY KEY(url, id_vote)
);

create table choices (
    id INTEGER primary key autoincrement,
    name TEXT not null,
    id_vote INTEGER not null,
    FOREIGN KEY(id_vote) REFERENCES vote(id)
);

-- Test data

insert into users (email, password, name, organization, is_admin, key) values ("admin@admin.fr", "admin", "Toto (admin) Tata", "World corp", 1, "test");
insert into roles (id, name, system) values (1, "Tous", 1);
insert into roles (name) values ("CA");
insert into roles (name) values ("Members");

