ALTER DATABASE pa SET timezone TO 'Australia/Victoria'; 

create table settings( id integer, import_path varchar, last_import_date float, constraint pk_settings_id primary key(id) );

create table file_type ( id integer, name varchar(32) unique, constraint pk_file_type_id primary key(id) );
insert into file_type values ( 1, 'Directory' );
insert into file_type values ( 2, 'Image' );
insert into file_type values ( 3, 'Video' );
insert into file_type values ( 4, 'Unknown' );

create table file( id integer, name varchar(128), size_MB integer, type varchar(20), path_prefix varchar(256), hash varchar(34), thumbnail varchar,
    constraint pk_photos_id primary key(id) );

create table person ( id integer, tag varchar(48), firstname varchar(48), surname varchar(48),
    constraint pk_person_id primary key(id) );

create table refimg ( id integer, fname varchar(256), constraint pk_refimg_id primary key(id) );

create table file_person_link ( file_id integer, person_id integer,
    constraint pk_fpl PRIMARY KEY(file_id, person_id), 
    constraint fk_fpl_file_id FOREIGN KEY (file_id) REFERENCES file(id), 
    constraint fk_fpl_person_id FOREIGN KEY (person_id) REFERENCES person(id) );

create table person_refimg_link ( person_id integer, refimg_id integer,
    constraint pk_prl PRIMARY KEY(person_id, refimg_id), 
    constraint fk_prl_person_id FOREIGN KEY (person_id) REFERENCES person(id), 
    constraint fk_prl_refimg_id FOREIGN KEY (refimg_id) REFERENCES refimg(id), 
    constraint u_prl_refimg_id unique(refimg_id) );

create table job ( 
    id integer, start_time timestamptz, last_update timestamptz, name varchar(64), state varchar(128), num_passes integer, current_pass integer,
    num_files integer, current_file_num integer, current_file varchar(256), wait_for integer, pa_job_state varchar(48),
    constraint pk_job_id primary key(id) );

create table joblog ( id integer, job_id integer, log_date timestamptz, log varchar, 
    constraint pk_jl_id primary key(id), constraint fk_jl_job_id foreign key(job_id) references job(id) );

create table pa_job_manager ( id integer, state varchar(128), num_active_jobs integer, num_completed_jobs integer, constraint pa_job_manager_id primary key(id) );

create table pa_job_manager_fe_message ( id integer, job_id integer, alert varchar(16), message varchar(1024), constraint pa_job_manager_fe_acks_id primary key(id), constraint fk_pa_job_manager_fe_message_job_id foreign key(job_id) references job(id) );

create sequence file_id_seq;
create sequence joblog_id_seq;
create sequence job_id_seq;
create sequence person_id_seq;
create sequence refimg_id_seq;
create sequence settings_id_seq;
create sequence pa_job_manager_id_seq;
create sequence pa_job_manager_fe_message_id_seq;

-- fake data only for making testing easier
insert into person values ( (select nextval('person_id_seq')), 'dad', 'Damien', 'De Paoli' );
insert into person values ( (select nextval('person_id_seq')), 'mum', 'Mandy', 'De Paoli' );
insert into person values ( (select nextval('person_id_seq')), 'cam', 'Cameron', 'De Paoli' );
insert into person values ( (select nextval('person_id_seq')), 'mich', 'Michelle', 'De Paoli' );
insert into refimg values ( (select nextval('refimg_id_seq')), 'dad.jpg');
insert into refimg values ( (select nextval('refimg_id_seq')), 'mum.jpg');
insert into refimg values ( (select nextval('refimg_id_seq')), 'cam.jpg');
insert into refimg values ( (select nextval('refimg_id_seq')), 'mich.jpg');
insert into person_refimg_link values ( 1, 1 );
insert into person_refimg_link values ( 2, 2 );
insert into person_refimg_link values ( 3, 3 );
insert into person_refimg_link values ( 4, 4 );
insert into settings values ( (select nextval('settings_id_seq')), '/home/ddp/src/photoassistant/images_to_process/#c:/Users/cam/Desktop/code/python/photoassistant/photos/#/home/ddp/src/photoassistant/new_img_dir/', 0 );
insert into job values ( (select nextval('job_id_seq')), now(), now(), 'Full Import', 'Completed', 3, 3, 157, 157, 'fake_data.img',   null, 'Completed' );
insert into joblog values ( (select nextval('joblog_id_seq')), 1, now(), 'Pass 1: Started Scanning Files' );
insert into joblog values ( (select nextval('joblog_id_seq')), 1, now(), 'Pass 1: Finished Scanning Files' );
insert into joblog values ( (select nextval('joblog_id_seq')), 1, now(), 'Pass 2: Started Generating Hashes and thumbnails' );
insert into joblog values ( (select nextval('joblog_id_seq')), 1, now(), 'Pass 2: Finished Generating Hashes and thumbnails' );
insert into joblog values ( (select nextval('joblog_id_seq')), 1, now(), 'Pass 3: Started Processing AI' );
insert into joblog values ( (select nextval('joblog_id_seq')), 1, now(), 'Pass 3: Finished Processing AI' );
