-- schema version 25 to 26

CREATE TABLE delivery_schedule (
    year        INTEGER NOT NULL,
    dsnr        INTEGER NOT NULL,

    date        TEXT    NOT NULL CHECK (date LIKE year || '-%' AND date REGEXP '^[1-9][0-9]{3}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])$'),
    zwstid      TEXT    NOT NULL,
    description TEXT    NOT NULL,
    max_weight  INTEGER,

    ancmt_from  INTEGER,
    ancmt_to    INTEGER,

    CONSTRAINT pk_delivery_schedule PRIMARY KEY (year, dsnr),
    CONSTRAINT fk_delivery_schedule_season FOREIGN KEY (year) REFERENCES season (year)
        ON UPDATE CASCADE
        ON DELETE RESTRICT,
    CONSTRAINT fk_delivery_schedule_branch FOREIGN KEY (zwstid) REFERENCES branch (zwstid)
        ON UPDATE CASCADE
        ON DELETE RESTRICT
) STRICT;

CREATE TABLE delivery_schedule_wine_variety (
    year     INTEGER NOT NULL,
    dsnr     INTEGER NOT NULL,
    sortid   TEXT    NOT NULL,

    priority INTEGER NOT NULL DEFAULT 1,

    CONSTRAINT pk_delivery_schedule_wine_variety PRIMARY KEY (year, dsnr, sortid),
    CONSTRAINT fk_delivery_schedule_wine_variety_delivery_schedule FOREIGN KEY (year, dsnr) REFERENCES delivery_schedule (year, dsnr)
        ON UPDATE CASCADE
        ON DELETE CASCADE,
    CONSTRAINT fk_delivery_schedule_wine_variety_wine_variety FOREIGN KEY (sortid) REFERENCES wine_variety (sortid)
        ON UPDATE CASCADE
        ON DELETE RESTRICT
) STRICT;

CREATE TABLE delivery_announcement (
    year   INTEGER NOT NULL,
    dsnr   INTEGER NOT NULL,
    mgnr   INTEGER NOT NULL,
    sortid TEXT    NOT NULL,

    weight INTEGER NOT NULL,

    type   TEXT    NOT NULL,
    ctime  INTEGER NOT NULL DEFAULT (UNIXEPOCH()),
    mtime  INTEGER NOT NULL DEFAULT (UNIXEPOCH()),

    CONSTRAINT pk_delivery_announcement PRIMARY KEY (year, dsnr, mgnr, sortid),
    CONSTRAINT fk_delivery_announcement_delivery_schedule FOREIGN KEY (year, dsnr) REFERENCES delivery_schedule (year, dsnr)
        ON UPDATE CASCADE
        ON DELETE CASCADE,
    CONSTRAINT fk_delivery_announcement_member FOREIGN KEY (mgnr) REFERENCES member (mgnr)
        ON UPDATE CASCADE
        ON DELETE CASCADE,
    CONSTRAINT fk_delivery_announcement_wine_variety FOREIGN KEY (sortid) REFERENCES wine_variety (sortid)
        ON UPDATE CASCADE
        ON DELETE RESTRICT
) STRICT;


CREATE TRIGGER t_delivery_announcement_i_ctime
    AFTER INSERT ON delivery_announcement FOR EACH ROW
    WHEN NEW.ctime != UNIXEPOCH()
BEGIN
    UPDATE delivery_announcement SET ctime = UNIXEPOCH() WHERE (year, dsnr, mgnr, sortid) = (NEW.year, NEW.dsnr, NEW.mgnr, NEW.sortid);
END;

CREATE TRIGGER t_delivery_announcement_u_ctime
    BEFORE UPDATE ON delivery_announcement FOR EACH ROW
    WHEN OLD.ctime != NEW.ctime
BEGIN
    SELECT RAISE(ABORT, 'It is not allowed to change ctime');
END;

CREATE TRIGGER t_delivery_announcement_i_mtime
    AFTER INSERT ON delivery_announcement FOR EACH ROW
    WHEN NEW.mtime != UNIXEPOCH()
BEGIN
    UPDATE delivery_announcement SET mtime = UNIXEPOCH() WHERE (year, dsnr, mgnr, sortid) = (NEW.year, NEW.dsnr, NEW.mgnr, NEW.sortid);
END;

CREATE TRIGGER t_delivery_announcement_u_mtime
    AFTER UPDATE ON delivery_announcement FOR EACH ROW
    WHEN NEW.mtime != UNIXEPOCH()
BEGIN
    UPDATE delivery_announcement SET mtime = UNIXEPOCH() WHERE (year, dsnr, mgnr, sortid) = (NEW.year, NEW.dsnr, NEW.mgnr, NEW.sortid);
END;
