alter database PA set timezone to 'Australia/Victoria'; 

create table SETTINGS( ID integer, IMPORT_PATH varchar, 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) );

create table ENTRY( ID integer, NAME varchar(128), TYPE_ID integer,
    constraint PK_ENTRY_ID primary key(ID),
    constraint FK_FILE_TYPE_TYPE_ID foreign key (TYPE_ID) references FILE_TYPE(ID) );

create table FILE ( EID integer, SIZE_MB integer, HASH varchar(34), THUMBNAIL varchar, 
    constraint PK_FILE_ID primary key(EID), 
    constraint FK_FILE_ENTRY_ID foreign key (EID) references ENTRY(ID) );

create table DIR ( EID integer, PATH_PREFIX varchar(256), NUM_FILES integer, LAST_IMPORT_DATE float, LAST_HASH_DATE float,
    constraint PK_DIR_EID primary key(EID),
    constraint FK_DIR_ENTRY_ID foreign key (EID) references ENTRY(ID) );

create table ENTRY_DIR_LINK ( entry_id integer, dir_eid integer,
    constraint PK_EDL_entry_id_dir_eid primary key (entry_id, dir_eid),
    constraint FK_EDL_ENTRY_ID foreign key (ENTRY_ID) references ENTRY(ID),
    constraint FK_EDL_DIR_EID foreign key (DIR_EID) references DIR(EID) );

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(EID), 
    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_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) );

-- used to pass / keep extra values, e.g. num_files for jobs that have sets of files, or out* for adding output from jobs that you want to pass to next job in the chain
create table JOBEXTRA ( ID integer, JOB_ID integer, NAME varchar(32), VALUE varchar(1024),
    constraint PK_JOBEXTRA_ID primary key(ID), constraint FK_JOBEXTRA_JOB_ID foreign key(JOB_ID) references JOB(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_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 FILE_TYPE_ID_SEQ;
create sequence JOBEXTRA_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;

insert into FILE_TYPE values ( (select nextval('FILE_TYPE_ID_SEQ')), 'Directory' );
insert into FILE_TYPE values ( (select nextval('FILE_TYPE_ID_SEQ')), 'Image' );
insert into FILE_TYPE values ( (select nextval('FILE_TYPE_ID_SEQ')), 'Video' );
insert into FILE_TYPE values ( (select nextval('FILE_TYPE_ID_SEQ')), 'Unknown' );

-- 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/' );
