# CREATE elections TABLE
#####################################

drop table if exists elections;
create table elections (
 id int NOT NULL auto_increment,
 name varchar(100) NOT NULL, 
 description TEXT NOT NULL, 
 anonymous tinyint NOT NULL DEFAULT 1, 
 startdate datetime, 
 enddate datetime NOT NULL, 
 active tinyint NOT NULL DEFAULT 0,
 user_id int NULL,
 quickvote tinyint NOT NULL DEFAULT 0,
 primary key (id),
 constraint fk_user_election foreign key (user_id) references users(id)
);

# CREATE candidates TABLE
#####################################

drop table if exists candidates;
create table candidates (
 id int NOT NULL auto_increment,
 election_id int NOT NULL,
 name varchar(100) NOT NULL, 
 description text NULL,
 picture_filename varchar(200),
 picture_data blob, 
 picture_type varchar(100), 
 primary key (id)
);

# CREATE voters TABLE
#####################################

drop table if exists voters;
create table voters (
 id int NOT NULL auto_increment,
 email varchar(100) NULL, 
 password varchar(100) NULL, 
 contacted tinyint NOT NULL DEFAULT 0, 
 election_id int NOT NULL, 
 session_id varchar(32) DEFAULT NULL,
 ipaddress varchar(32) DEFAULT NULL,
 constraint fk_election_voter foreign key (election_id) references election(id),
 primary key (id)
);


# CREATE tokens TABLE
#####################################

drop table if exists tokens;
create table tokens(
 id int NOT NULL auto_increment,
 token varchar(100) NOT NULL, 
 vote_id int NOT NULL, 
 constraint fk_vote_token foreign key (vote_id) references vote(id),
 primary key (id)
);

# CREATE votes TABLE
#####################################

drop table if exists votes;
create table votes (
 id int NOT NULL auto_increment,
 voter_id int DEFAULT NULL,
 confirmed tinyint NOT NULL DEFAULT 0,
 time      datetime         DEFAULT NULL,
 constraint fk_vote_voter foreign key (voter_id) references voters(id),
 primary key (id)
);

# CREATE rankings TABLE
#####################################

drop table if exists rankings;
create table rankings (
 id int NOT NULL auto_increment,
 vote_id int DEFAULT NULL,
 candidate_id int DEFAULT NULL,
 rank int DEFAULT NULL,
 primary key (id)
);

# CREATE users TABLE
#####################################
#DROP TABLE IF EXISTS `users`;
#CREATE TABLE `users` (
#  `id` int(11) NOT NULL auto_increment,
#  `login` varchar(80) NOT NULL default '',
#  `salted_password` varchar(40) NOT NULL default '',
#  `email` varchar(60) NOT NULL default '',
#  `firstname` varchar(40) default NULL,
#  `lastname` varchar(40) default NULL,
#  `salt` varchar(40) NOT NULL default '',
#  `verified` int(11) default '0',
#  `role` varchar(40) default NULL,
#  `security_token` varchar(40) default NULL,
#  `token_expiry` datetime default NULL,
#  `created_at` datetime default NULL,
#  `updated_at` datetime default NULL,
#  `logged_in_at` datetime default NULL,
#  `deleted` int(11) default '0',
#  `delete_after` datetime default NULL,
#  PRIMARY KEY  (`id`)
#) ENGINE=InnoDB DEFAULT CHARSET=latin1;

#Following is the new users table that goes with acts_as_authenticated
#Is simpler for now, saving the old table while in transition between 
#the two for ideas on what attributes may be helpful/necessary
drop table if exists users;
create table users (
	id                        int         not null auto_increment,
	login                     text,
	ip                        text not null,
	email                     text,
	crypted_password          varchar(40),
	salt                      varchar(40),
	created_at                datetime,
	updated_at                datetime,
	remember_token            text,
	remember_token_expires_at datetime,
	primary key(id)
);