<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
        xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.0.xsd
        http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">
    <changeSet id="2" author="jlim">
        <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
            <![CDATA[
            USE art_relations;;

            DROP TRIGGER IF EXISTS before_update_release;;

            CREATE DEFINER = 'theorchard'@'10.10.%' TRIGGER before_update_release BEFORE UPDATE ON releases FOR EACH ROW
            before_update_release_block: BEGIN

                DECLARE projectCode VARCHAR(255) DEFAULT NULL;
                DECLARE projectID_VCN INT DEFAULT NULL;
                DECLARE newProjectCode VARCHAR(255) DEFAULT NULL;
                DECLARE vendorId INT DEFAULT NULL;
                DECLARE projectId INT DEFAULT NULL;

                IF (OLD.vendor_catalog_number <=> LEFT(NEW.vendor_catalog_number,20)) THEN
                    LEAVE before_update_release_block;
                END IF;

                -- exit if release is in_content
                IF (OLD.release_status = "in_content") THEN
                    LEAVE before_update_release_block;
                END IF;

                SET vendorId = (SELECT v.vendor_id FROM vendor v INNER JOIN artist_info ai ON ai.vendor_id = v.vendor_id WHERE ai.artist_id = NEW.artist_id);

                IF (vendorId IS NULL) THEN
                    LEAVE before_update_release_block;
                END IF;

                -- get potential project_id with VCN
                SET projectID_VCN = (SELECT p.project_id FROM art_relations.project AS p WHERE p.vendor_id = vendorId AND p.subaccount_id = IFNULL(NEW.subaccount_id ,0) AND p.project_code = NEW.vendor_catalog_number);

                IF (NEW.vendor_catalog_number IS NULL OR TRIM(NEW.vendor_catalog_number) = "") THEN
                    -- If vcn is null and no projects exist with the vcn and upc, then set project code to the upc
                    SET newProjectCode = NEW.upc;
                ELSE
                    IF (projectID_VCN IS NULL) THEN
                        SET newProjectCode = NEW.vendor_catalog_number;
                    ELSE
                        -- if vcn is not null but a project already exists with that vcn, then use that project id
                        SET NEW.project_id = projectID_VCN;
                    END IF;
                END IF;

                -- if newProjectCode has been set, we need to create a new project and update the project_id of the incoming release
                IF (newProjectCode IS NOT NULL) THEN
                    -- using insert ignore just in case another project with the same key combination was inserted previously
                    INSERT IGNORE INTO art_relations.project SET project_name = IFNULL(NEW.release_name,'Unnamed Project'), project_code = newProjectCode, vendor_id = vendorId, subaccount_id = IFNULL(NEW.subaccount_id, 0), created_date_utc = UTC_TIMESTAMP(), updated_date_utc = UTC_TIMESTAMP();
                    SET projectId = (SELECT p.project_id FROM art_relations.project AS p WHERE p.vendor_id = vendorId AND p.subaccount_id = IFNULL(NEW.subaccount_id ,0) AND p.project_code = newProjectCode);
                    SET NEW.project_id = projectId;
                END IF;

            END before_update_release_block;;
            ]]>
        </sql>
        <rollback>
            <sql dbms="mysql" endDelimiter=";;" splitStatements="true" stripComments="true">
                USE art_relations;;
                DROP TRIGGER IF EXISTS before_update_release;;
            </sql>
        </rollback>
    </changeSet>
</databaseChangeLog>
