#!/usr/bin/perl

use strict;
use lib '/home/slowtwitch/forum.slowtwitch.com/cgi-bin/admin';
use GForum qw/$DB $CFG/;

GForum::init('/home/slowtwitch/forum.slowtwitch.com/cgi-bin/admin');

main();

sub main {
# Create the Poll table.
    my $p = $DB->creator ('Poll');
    $p->cols([
	poll_id       => { type => 'INT', not_null => 1 },
	poll_question => { type => 'VARCHAR',size => '255',not_null => 1},
	poll_answer   => { type => 'VARCHAR',size => '255',not_null => 1},
	poll_type     => { type => 'TINYINT',default => '1',not_null => '1'},
	poll_votes    => { type => 'INT',not_null => 1},
	poll_enabled  => { type => 'TINYINT',default => '1',not_null => 1},  
	poll_date     => { type => 'Date', not_null => 1},
	poll_text     => { type => 'TEXT', not_null => 0},
	poll_home     => { type => 'TINYINT',default => '0',not_null => 1},  
    ]);

    $p->pk('poll_id');
    $p->ai('poll_id');

    if (!$p->create and $GT::SQL::errcode eq 'TBLEXISTS') {
	$p->set_defaults();
	$p->save_schema();
    }

# Create the PollAnswer table
    my $a = $DB->creator ('PollAnswer');
    $a->cols([
	poll_answer_id => { type => 'INT', not_null => 1 },
	poll_id_fk => { type => 'INT', not_null => 1 },
	poll_answer_answer => {type => 'VARCHAR',size => '255',not_null => 1,default => 0},
	poll_answer_votes => {type => 'INT',not_null => 1,default => 0}
    ]);

    $a->pk('poll_answer_id');
    $a->ai('poll_answer_id');

    $a->fk({
	Poll => { poll_id_fk => 'poll_id' },
    });

    $a->index({ a_pl => ['poll_id_fk']});

    if (!$a->create and $GT::SQL::errcode eq 'TBLEXISTS') {
	$a->set_defaults();
	$a->save_schema();
    }

# Create the PollVote table
    my $v = $DB->creator ('PollVote');
    $v->cols([
	poll_vote_id => { type => 'INT', not_null => 1 },
	poll_id_fk => { type => 'INT', not_null => 1 },
	poll_vote_ip => { type => 'VARCHAR',size => '15', not_null => 1 },
	poll_vote_time => { type => 'INT', default => '0', not_null => '1' }
    ]);

    $v->pk('poll_vote_id');
    $v->ai('poll_vote_id');

    $v->fk ( {
	'Poll' => {
	    'poll_id_fk' => 'poll_id'
	},
    });

    $v->index({ v_p => ['poll_id_fk'] });

    if (!$v->create and $GT::SQL::errcode eq 'TBLEXISTS') {
	$v->set_defaults();
	$v->save_schema();
    }

}

