-- meta tables, like units and certificates
create table if not exists `legal_base`
(
    `id` 					INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    `legal_base`			VARCHAR(255),
    `journal` 				VARCHAR(255),
    `page`					INT,
    `date`					DATE
);

create table if not exists `monetary_unit`
(
    `id` 						INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    `monetary_unit_code` 		CHAR(3)
);

create table if not exists `unit`
(
    `id` 					INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    `unit_code`				CHAR(3),
    `unit_qualifier`		CHAR(1),
    `label`					TEXT
);

create table if not exists `certificate_type`
(
    `id` 						INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    `certificate_type_code`	CHAR(1)
);

create table if not exists `condition_type`
(
    `id` 								INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    `condition_type_code`				CHAR(2)
);

create table if not exists `certificate`
(
    `id` 								INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    `certificate_code`					CHAR(4),
    `certificate_type_id`               INT NOT NULL,
    `start_date`						DATE,
    `end_date`							DATE,
    FOREIGN KEY (certificate_type_id)	REFERENCES certificate_type(id)
);

-- measure meta infos
create table if not exists `footnote`
(
    id 									INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    footnote 							CHAR(8),
    start_date 							DATE,
    end_date							DATE
);

create table if not exists `measure_series`
(
    id 									INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    measure_series_code					CHAR(1)
);

create table if not exists `measure`
(
    id 									INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    measure_series_id					INT NOT NULL,
    measure_code						CHAR(3),
    short_desc							TEXT,
    tm_code								TINYINT,
    start_date 							DATE,
    FOREIGN KEY (measure_series_id)		REFERENCES measure_series(id)
);

create table if not exists `measure_action`
(
    id 									INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    measure_action_code					CHAR(2),
    CONSTRAINT UC_MeasureAction UNIQUE (measure_action_code)
);

-- geo
create table if not exists `geo`
(
    id 									INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    iso_3166_code						CHAR(2),
    start_date							DATE,
    end_date							DATE,
    CONSTRAINT UC_IsoCode UNIQUE (iso_3166_code)
);

create table if not exists `geo_group`
(
    id 									INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    geo_group_code						CHAR(4),
    abbr								TEXT,
    start_date							DATE,
    end_date							DATE,
    CONSTRAINT UC_GroupCode UNIQUE (geo_group_code)
);

create table if not exists `geo_group_membership`
(
    id 									INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    geo_id								INT NOT NULL,
    geo_group_id						INT NOT NULL,
    start_date							DATE,
    end_date							DATE,
    FOREIGN KEY (geo_id)				REFERENCES geo(id),
    FOREIGN KEY (geo_group_id)			REFERENCES geo_group(id),
    CONSTRAINT UC_GeoGroupTuple UNIQUE (geo_id, geo_group_id)
);

-- nomenclature
create table if not exists `nomenclature`
(
    id 									INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    hscode								CHAR(10),
    suffix								CHAR(2),
    hierachy							TINYINT,
    indent								TINYINT,
    start_date							DATE,
    end_date							DATE,
    is_leaf								BOOLEAN,
    is_leaf_start_date					DATE,
    parent_id							INT NULL -- TODO: for each child do: alter table nomenclature add constraint parent_nomenclature foreign key (parent_id) references nomenclature(id) on delete set null;
);

create table if not exists `nomenclature_footnote`
(
    id 									INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    footnote_id							INT NOT NULL,
    nomenclature_id						INT NOT NULL,
    FOREIGN KEY (footnote_id)			REFERENCES footnote(id),
    FOREIGN KEY (nomenclature_id)		REFERENCES nomenclature(id)
);

create table if not exists `additional_code`
(
    id 									INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    additional_code						CHAR(4),
    start_date							DATE,
    end_date							DATE,
    CONSTRAINT UC_AdditionalCode UNIQUE (additional_code)
);

-- import, applied_measures
create table if not exists `import`
(
    id 									INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    nomenclature_id						INT NOT NULL,
    additional_code_id					INT DEFAULT NULL,
    geo_id								INT DEFAULT NULL,
    geo_group_id						INT DEFAULT NULL,
    FOREIGN KEY (nomenclature_id)		REFERENCES nomenclature(id),
    FOREIGN KEY (additional_code_id)	REFERENCES additional_code(id),
    FOREIGN KEY (geo_id)				REFERENCES geo(id),
    FOREIGN KEY (geo_group_id)			REFERENCES geo_group(id)
);

create table if not exists `applied_measure`
(
    id 									INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    import_id							INT NOT NULL,
    measure_id							INT NOT NULL,
    legal_base_id						INT DEFAULT NULL,
    legal_base							TEXT DEFAULT NULL,
    start_date							DATE DEFAULT NULL,
    end_date							DATE DEFAULT NULL,
    amount								TEXT DEFAULT NULL,
    order_number						INT DEFAULT NULL,
    FOREIGN KEY (import_id)				REFERENCES import(id),
    FOREIGN KEY (measure_id)			REFERENCES measure(id),
    FOREIGN KEY (legal_base_id)			REFERENCES legal_base(id)
);

create table if not exists `measure_footnote`
(
    id 									INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    footnote_id							INT NOT NULL,
    applied_measure_id					INT NOT NULL,
    start_date							DATE DEFAULT NULL,
    end_date							DATE DEFAULT NULL,
    FOREIGN KEY (footnote_id)			REFERENCES footnote(id),
    FOREIGN KEY (applied_measure_id)	REFERENCES applied_measure(id)
);

create table if not exists `applied_measure_condition`
(
    id 									INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    sequence_no							INT DEFAULT NULL,
    applied_measure_id					INT NOT NULL,
    measure_action_id					INT NOT NULL,
    monetary_unit_id					INT DEFAULT NULL,
    unit_id								INT DEFAULT NULL,
    certificate_id						INT DEFAULT NULL,
    condition_type_id					INT NOT NULL,
    amount								TEXT,
    start_date							DATE DEFAULT NULL,
    end_date							DATE DEFAULT NULL,
    FOREIGN KEY (applied_measure_id)	REFERENCES applied_measure(id),
    FOREIGN KEY (measure_action_id)		REFERENCES measure_action(id),
    FOREIGN KEY (monetary_unit_id)		REFERENCES monetary_unit(id),
    FOREIGN KEY (unit_id)				REFERENCES unit(id),
    FOREIGN KEY (certificate_id)		REFERENCES certificate(id),
    FOREIGN KEY (condition_type_id)		REFERENCES condition_type(id)
);

create table if not exists `measure_exclusion`
(
    id 									INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    geo_id								INT NOT NULL,
    applied_measure_id					INT NOT NULL,
    start_date							DATE DEFAULT NULL,
    end_date							DATE DEFAULT NULL,
    FOREIGN KEY (geo_id)				REFERENCES geo(id),
    FOREIGN KEY (applied_measure_id)	REFERENCES applied_measure(id)
);

-- descriptions
create table if not exists `footnotes_desc`
(
    id 						INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    ref_id					INT NOT NULL,
    lang					CHAR(2),
    `desc`					TEXT,
    desc_start_date			DATE,
    FOREIGN KEY (ref_id)	REFERENCES footnote(id)
);

create table if not exists `measure_desc`
(
    id 						INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    ref_id					INT NOT NULL,
    lang					CHAR(2),
    `desc`					TEXT,
    desc_start_date			DATE,
    FOREIGN KEY (ref_id)	REFERENCES measure(id)
);

create table if not exists `measure_series_desc`
(
    id 						INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    ref_id					INT NOT NULL,
    lang					CHAR(2),
    `desc`					TEXT,
    desc_start_date			DATE,
    FOREIGN KEY (ref_id)	REFERENCES measure_series(id)
);

create table if not exists `measure_action_desc`
(
    id 						INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    ref_id					INT NOT NULL,
    lang					CHAR(2),
    `desc`					TEXT,
    desc_start_date			DATE,
    FOREIGN KEY (ref_id)	REFERENCES measure_action(id)
);

create table if not exists `geo_desc`
(
    id 						INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    ref_id					INT NOT NULL,
    lang					CHAR(2),
    `desc`					TEXT,
    desc_start_date			DATE,
    FOREIGN KEY (ref_id)	REFERENCES geo(id)
);

create table if not exists `geo_group_desc`
(
    id 						INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    ref_id					INT NOT NULL,
    lang					CHAR(2),
    `desc`					TEXT,
    desc_start_date			DATE,
    FOREIGN KEY (ref_id)	REFERENCES geo_group(id)
);

create table if not exists `monetary_unit_desc`
(
    id 						INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    ref_id					INT NOT NULL,
    lang					CHAR(2),
    `desc`					TEXT,
    desc_start_date			DATE,
    FOREIGN KEY (ref_id)	REFERENCES monetary_unit(id)
);

create table if not exists `unit_desc`
(
    id 						INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    ref_id					INT NOT NULL,
    lang					CHAR(2),
    `desc`					TEXT,
    desc_start_date			DATE,
    FOREIGN KEY (ref_id)	REFERENCES unit(id)
);

create table if not exists `certificate_desc`
(
    id 						INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    ref_id					INT NOT NULL,
    lang					CHAR(2),
    `desc`					TEXT,
    desc_start_date			DATE,
    FOREIGN KEY (ref_id)	REFERENCES certificate(id)
);

create table if not exists `certificate_type_desc`
(
    id 						INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    ref_id					INT NOT NULL,
    lang					CHAR(2),
    `desc`					TEXT,
    desc_start_date			DATE,
    FOREIGN KEY (ref_id)	REFERENCES certificate_type(id)
);

create table if not exists `condition_type_desc`
(
    id 						INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    ref_id					INT NOT NULL,
    lang					CHAR(2),
    `desc`					TEXT,
    desc_start_date			DATE,
    FOREIGN KEY (ref_id)	REFERENCES condition_type(id)
);

create table if not exists `additional_code_desc`
(
    id 						INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    ref_id					INT NOT NULL,
    lang					CHAR(2),
    `desc`					TEXT,
    desc_start_date			DATE,
    FOREIGN KEY (ref_id)	REFERENCES additional_code(id)
);

create table if not exists `nomenclature_desc`
(
    id 						INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    ref_id					INT NOT NULL,
    lang					CHAR(2),
    `desc`					TEXT,
    desc_start_date			DATE,
    FOREIGN KEY (ref_id)	REFERENCES nomenclature(id)
);



