-- schema version 37 to 38

UPDATE client_parameter SET value = '0' WHERE param = 'ENABLE_TIME_TRIGGERS';

DROP TRIGGER t_area_commitment_i_ctime;
DROP TRIGGER t_area_commitment_u_ctime;
DROP TRIGGER t_area_commitment_i_mtime;
DROP TRIGGER t_area_commitment_u_mtime;

CREATE TABLE area_commitment_contract (
    fbnr      INTEGER NOT NULL,

    kgnr      INTEGER NOT NULL,
    rdnr      INTEGER,

    comment   TEXT             DEFAULT NULL,
    ctime     INTEGER NOT NULL DEFAULT (UNIXEPOCH()),
    mtime     INTEGER NOT NULL DEFAULT (UNIXEPOCH()),
    xtime     INTEGER          DEFAULT NULL,
    itime     INTEGER          DEFAULT NULL,

    CONSTRAINT area_commitment_contract PRIMARY KEY (fbnr),
    CONSTRAINT fk_area_commitment_contract_wb_kg FOREIGN KEY (kgnr) REFERENCES wb_kg (kgnr)
        ON UPDATE CASCADE
        ON DELETE RESTRICT,
    CONSTRAINT fk_area_commitment_contract_wb_rd FOREIGN KEY (kgnr, rdnr) REFERENCES wb_rd (kgnr, rdnr)
        ON UPDATE CASCADE
        ON DELETE RESTRICT
) STRICT;

CREATE TABLE area_commitment_new (
    fbnr      INTEGER NOT NULL,
    revnr     INTEGER NOT NULL,
    mgnr      INTEGER NOT NULL,

    vtrgid    TEXT    NOT NULL,
    cultid    TEXT             DEFAULT NULL,
    area      INTEGER NOT NULL,
    gstnr     TEXT    NOT NULL,

    year_from INTEGER CHECK (year_from >= 1000 AND year_from <= 9999) DEFAULT NULL,
    year_to   INTEGER CHECK (year_to   >= 1000 AND year_to   <= 9999) DEFAULT NULL,

    ctime     INTEGER NOT NULL DEFAULT (UNIXEPOCH()),
    mtime     INTEGER NOT NULL DEFAULT (UNIXEPOCH()),
    xtime     INTEGER          DEFAULT NULL,
    itime     INTEGER          DEFAULT NULL,

    CONSTRAINT pk_area_commitment PRIMARY KEY (fbnr, revnr),
    CONSTRAINT fk_area_commitment_area_commitment_contract FOREIGN KEY (fbnr) REFERENCES area_commitment_contract (fbnr)
        ON UPDATE CASCADE
        ON DELETE CASCADE,
    CONSTRAINT fk_area_commitment_member FOREIGN KEY (mgnr) REFERENCES member (mgnr)
        ON UPDATE CASCADE
        ON DELETE RESTRICT,
    CONSTRAINT fk_area_commitment_area_commitment_type FOREIGN KEY (vtrgid) REFERENCES area_commitment_type (vtrgid)
        ON UPDATE CASCADE
        ON DELETE RESTRICT,
    CONSTRAINT fk_area_commitment_wine_cultivation FOREIGN KEY (cultid) REFERENCES wine_cultivation (cultid)
        ON UPDATE CASCADE
        ON DELETE RESTRICT
) STRICT;

INSERT INTO area_commitment_contract (fbnr, kgnr, rdnr, comment, ctime, mtime, xtime, itime)
SELECT fbnr, kgnr, rdnr, comment, ctime, mtime, xtime, itime
FROM area_commitment;

INSERT INTO area_commitment_new (fbnr, revnr, mgnr, vtrgid, cultid, area, gstnr, year_from, year_to, ctime, mtime, xtime, itime)
SELECT fbnr, 1, mgnr, vtrgid, cultid, area, gstnr, year_from, year_to, ctime, mtime, xtime, itime
FROM area_commitment;

PRAGMA foreign_keys = OFF;
PRAGMA writable_schema = ON;
DROP TABLE area_commitment;
ALTER TABLE area_commitment_new RENAME TO area_commitment;
PRAGMA writable_schema = OFF;
PRAGMA foreign_keys = ON;

CREATE TRIGGER t_area_commitment_contract_i_ctime
    AFTER INSERT ON area_commitment_contract FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1 AND NEW.ctime != UNIXEPOCH()
BEGIN
    UPDATE area_commitment_contract SET ctime = UNIXEPOCH() WHERE fbnr = NEW.fbnr;
END;

CREATE TRIGGER t_area_commitment_contract_u_ctime
    BEFORE UPDATE ON area_commitment_contract FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1 AND OLD.ctime != NEW.ctime
BEGIN
    SELECT RAISE(ABORT, 'It is not allowed to change ctime');
END;

CREATE TRIGGER t_area_commitment_contract_i_mtime
    AFTER INSERT ON area_commitment_contract FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1 AND NEW.mtime != UNIXEPOCH()
BEGIN
    UPDATE area_commitment_contract SET mtime = UNIXEPOCH() WHERE fbnr = NEW.fbnr;
END;

CREATE TRIGGER t_area_commitment_contract_u_mtime
    AFTER UPDATE ON area_commitment_contract FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1 AND NEW.mtime != UNIXEPOCH()
BEGIN
    UPDATE area_commitment_contract SET mtime = UNIXEPOCH() WHERE fbnr = NEW.fbnr;
END;

CREATE TRIGGER t_area_commitment_i_ctime
    AFTER INSERT ON area_commitment FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1 AND NEW.ctime != UNIXEPOCH()
BEGIN
    UPDATE area_commitment SET ctime = UNIXEPOCH() WHERE (fbnr, revnr) = (NEW.fbnr, NEW.revnr);
END;

CREATE TRIGGER t_area_commitment_u_ctime
    BEFORE UPDATE ON area_commitment FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1 AND OLD.ctime != NEW.ctime
BEGIN
    SELECT RAISE(ABORT, 'It is not allowed to change ctime');
END;

CREATE TRIGGER t_area_commitment_i_mtime
    AFTER INSERT ON area_commitment FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1 AND NEW.mtime != UNIXEPOCH()
BEGIN
    UPDATE area_commitment SET mtime = UNIXEPOCH() WHERE (fbnr, revnr) = (NEW.fbnr, NEW.revnr);
END;

CREATE TRIGGER t_area_commitment_u_mtime
    AFTER UPDATE ON area_commitment FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1 AND NEW.mtime != UNIXEPOCH()
BEGIN
    UPDATE area_commitment SET mtime = UNIXEPOCH() WHERE (fbnr, revnr) = (NEW.fbnr, NEW.revnr);
END;

CREATE TRIGGER t_area_commitment_i_mtime_contract
    AFTER INSERT ON area_commitment FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1
BEGIN
    UPDATE area_commitment_contract SET mtime = UNIXEPOCH() WHERE fbnr = NEW.fbnr;
END;

CREATE TRIGGER t_area_commitment_u_mtime_contract
    AFTER UPDATE ON area_commitment FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1
BEGIN
    UPDATE area_commitment_contract SET mtime = UNIXEPOCH() WHERE fbnr = NEW.fbnr OR fbnr = OLD.fbnr;
END;

CREATE TRIGGER t_area_commitment_d_mtime_contract
    AFTER DELETE ON area_commitment FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1
BEGIN
    UPDATE area_commitment_contract SET mtime = UNIXEPOCH() WHERE fbnr = OLD.fbnr;
END;

CREATE TRIGGER t_area_commitment_i_mtime_member
    AFTER INSERT ON area_commitment FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1
BEGIN
    UPDATE member  SET mtime = UNIXEPOCH() WHERE mgnr = NEW.mgnr;
END;

CREATE TRIGGER t_area_commitment_u_mtime_member
    AFTER UPDATE ON area_commitment FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1
BEGIN
    UPDATE member SET mtime = UNIXEPOCH() WHERE mgnr = NEW.mgnr OR mgnr = OLD.mgnr;
END;

CREATE TRIGGER t_area_commitment_d_mtime_member
    AFTER DELETE ON area_commitment FOR EACH ROW
    WHEN (SELECT value FROM client_parameter WHERE param = 'ENABLE_TIME_TRIGGERS') = 1
BEGIN
    UPDATE member SET mtime = UNIXEPOCH() WHERE mgnr = OLD.mgnr;
END;

-- fix user fiddling - set actual year_from
UPDATE area_commitment AS b
SET fbnr = a.fbnr, revnr = b.fbnr, year_from = a.year_to + 1
FROM area_commitment AS a
WHERE a.fbnr < b.fbnr
  AND COALESCE(a.year_from <= a.year_to, TRUE)
  AND COALESCE(b.year_from <= b.year_to, TRUE)
  AND SUBSTR(a.vtrgid, 0, 2) = SUBSTR(b.vtrgid, 0, 2)
  AND (SELECT c.kgnr = d.kgnr AND COALESCE(c.rdnr = d.rdnr, TRUE) FROM area_commitment_contract c, area_commitment_contract d WHERE c.fbnr = a.fbnr AND d.fbnr = b.fbnr)
  AND ((IIF(INSTR(a.gstnr, b.gstnr) > 0, 1, 0) + IIF(INSTR(b.gstnr, a.gstnr) > 0, 1, 0) + IIF(a.area = b.area, 2, 0) + (a.mgnr = b.mgnr)) > 1)
  AND ((a.year_from = b.year_from AND b.year_to IS NULL AND a.year_to IS NOT NULL));

-- simple
UPDATE area_commitment AS b
SET fbnr = a.fbnr, revnr = b.fbnr
FROM area_commitment AS a
WHERE a.fbnr < b.fbnr
  AND COALESCE(a.year_from <= a.year_to, TRUE)
  AND COALESCE(b.year_from <= b.year_to, TRUE)
  AND SUBSTR(a.vtrgid, 0, 2) = SUBSTR(b.vtrgid, 0, 2)
  AND (SELECT c.kgnr = d.kgnr AND COALESCE(c.rdnr = d.rdnr, TRUE) FROM area_commitment_contract c, area_commitment_contract d WHERE c.fbnr = a.fbnr AND d.fbnr = b.fbnr)
  AND (a.gstnr = b.gstnr OR a.gstnr IN ('offen', '', '-')) AND a.area = b.area
  AND a.year_to + 1 = b.year_from;

-- copy comments
UPDATE area_commitment_contract AS b
SET comment = IIF(b.comment IS NULL, a.comment, CONCAT(b.comment, '; ', a.comment))
FROM area_commitment_contract AS a
WHERE a.comment IS NOT NULL AND a.fbnr IN (SELECT revnr FROM area_commitment WHERE revnr > 1 AND fbnr = b.fbnr);

-- fix revnr
UPDATE area_commitment AS b
SET revnr = a.revnr + 1
FROM area_commitment AS a
WHERE a.fbnr = b.fbnr AND a.revnr < b.revnr;
UPDATE area_commitment AS b
SET revnr = a.revnr + 1
FROM area_commitment AS a
WHERE a.fbnr = b.fbnr AND a.revnr < b.revnr;
UPDATE area_commitment AS b
SET revnr = a.revnr + 1
FROM area_commitment AS a
WHERE a.fbnr = b.fbnr AND a.revnr < b.revnr;

-- fix year_from
UPDATE area_commitment AS b
SET year_from = a.year_to + 1
FROM area_commitment AS a
WHERE a.fbnr = b.fbnr AND a.revnr = b.revnr - 1
  AND a.year_to = b.year_from;
UPDATE area_commitment AS b
SET year_from = a.year_to + 1
FROM area_commitment AS a
WHERE a.fbnr = b.fbnr AND a.revnr = b.revnr - 1
  AND a.year_to = b.year_from;

-- delete unreferenced contracts
DELETE FROM area_commitment_contract
WHERE fbnr IN (SELECT c.fbnr FROM area_commitment_contract c
                           LEFT JOIN area_commitment a ON a.fbnr = c.fbnr
               WHERE a.fbnr IS NULL);

UPDATE client_parameter SET value = '1' WHERE param = 'ENABLE_TIME_TRIGGERS';
