DROP TABLE IF EXISTS ast_sub_t;
DROP TABLE IF EXISTS ast_sub_type_t;
DROP TABLE IF EXISTS ast_filter_t;
DROP TABLE IF EXISTS ast_filter_type_t;

CREATE TABLE ast_sub_type_t
(
    id INT PRIMARY KEY,
    nm VARCHAR(8) NOT NULL
) ENGINE = INNODB;

INSERT INTO ast_sub_type_t
VALUES (1, 'light');
INSERT INTO ast_sub_type_t
VALUES (2, 'dark');
INSERT INTO ast_sub_type_t
VALUES (3, 'flat');
INSERT INTO ast_sub_type_t
VALUES (4, 'bias');

CREATE TABLE ast_filter_type_t
(
    id INT PRIMARY KEY,
    nm VARCHAR(16) NOT NULL
) ENGINE = INNODB;

INSERT INTO ast_filter_type_t (id, nm)
VALUES (1, 'red');
INSERT INTO ast_filter_type_t (id, nm)
VALUES (2, 'green');
INSERT INTO ast_filter_type_t (id, nm)
VALUES (3, 'blue');
INSERT INTO ast_filter_type_t (id, nm)
VALUES (4, 'ha');
INSERT INTO ast_filter_type_t (id, nm)
VALUES (5, 'sii');
INSERT INTO ast_filter_type_t (id, nm)
VALUES (6, 'oiii');
INSERT INTO ast_filter_type_t (id, nm)
VALUES (7, 'lum');

CREATE TABLE ast_filter_t
(
    id             INT PRIMARY KEY AUTO_INCREMENT,
    serial         VARCHAR(16) NOT NULL,
    filter_type_id INT         NOT NULL,
    size_mm        INT         NOT NULL,
    width_nm       INT,
    FOREIGN KEY (filter_type_id) REFERENCES ast_filter_type_t (id)
) ENGINE = INNODB;

INSERT INTO ast_filter_t (id, serial, filter_type_id, size_mm)
VALUES (1, '9-1006', 1, 36);
INSERT INTO ast_filter_t (id, serial, filter_type_id, size_mm)
VALUES (2, '9-1007', 2, 36);
INSERT INTO ast_filter_t (id, serial, filter_type_id, size_mm)
VALUES (3, '9-1008', 3, 36);
INSERT INTO ast_filter_t (id, serial, filter_type_id, size_mm, width_nm)
VALUES (4, '9-1010', 4, 36, 3);
INSERT INTO ast_filter_t (id, serial, filter_type_id, size_mm, width_nm)
VALUES (5, '9-1012', 5, 36, 3);
INSERT INTO ast_filter_t (id, serial, filter_type_id, size_mm, width_nm)
VALUES (6, '9-1011', 6, 36, 3);
INSERT INTO ast_filter_t (id, serial, filter_type_id, size_mm)
VALUES (7, '9-1009', 7, 36);

DROP TABLE IF EXISTS ast_sub_t;
CREATE TABLE ast_sub_t
(
    id             binary(16) primary key,
    capture_ts     TIMESTAMP NOT NULL,
    sub_type_id    int       NOT NULL,
    FOREIGN KEY (sub_type_id) REFERENCES ast_sub_type_t (id),
    filter_id      int       NOT NULL,
    FOREIGN KEY (filter_id) REFERENCES ast_filter_t (id),
    length_sec     DOUBLE    NOT NULL,
    gain           INT       NOT NULL,
    off            INT       NOT NULL,
    width_px       INT       NOT NULL,
    height_px      INT       NOT NULL,
    ccd_temp       DOUBLE    NOT NULL,
    center_ra_deg  DOUBLE    NOT NULL,
    center_dec_deg DOUBLE    NOT NULL
) ENGINE = INNODB;