const String tableLicense = 'license';

class LicenseConstants {
  static String get createLicenseTable => '''
      CREATE TABLE IF NOT EXISTS $tableLicense (
        key TEXT UNIQUE,
        display TEXT,
        expirationDate TEXT,
        startDate TEXT,
        quantity TEXT
      );
    ''';

  static String get deleteLicenseTable => 'DROP TABLE IF EXISTS $tableLicense;';

  static String get updatePetsHistoryTrigger => '''
      CREATE TRIGGER update_fre_hub_pets_history
      AFTER INSERT ON $tableLicense
      WHEN NEW.key = 'FRE-HUB-PETS'
      BEGIN
        INSERT OR REPLACE INTO $tableLicense (key, display, expirationDate, startDate, quantity)
        VALUES ('FRE-HUB-PETS-HISTORY', NEW.display, NEW.expirationDate, NEW.startDate, NEW.quantity);
      END;
    ''';

  static String get dropPetsHistoryTrigger =>
      'DROP TRIGGER IF EXISTS update_fre_hub_pets_history;';

  static String get updateDisplayTrigger => '''
      CREATE TRIGGER update_display_trigger
      AFTER UPDATE ON $tableLicense
      WHEN NEW.key IN ('FRE-HUB-OPENED-VISITS', 'FRE-HUB-VEHICLES', 'FRE-HUB-RESIDENTS')
      BEGIN
        INSERT OR REPLACE INTO $tableLicense (key, display, expirationDate, startDate, quantity)
        VALUES (
          'FRE-HUB-ABOUT-PROPERTY',
          CASE
            WHEN (SELECT COUNT(*) FROM $tableLicense WHERE key IN ('FRE-HUB-OPENED-VISITS', 'FRE-HUB-VEHICLES', 'FRE-HUB-RESIDENTS') AND display = 'VISIVEL') > 0
            THEN 'VISIVEL'
            ELSE 'INVISIVEL'
          END,
          NULL, NULL, NULL
        );
      END;
    ''';

  static String get dropUpdateDisplayTrigger =>
      'DROP TRIGGER IF EXISTS update_display_trigger;';

  static String get insertDisplayTrigger => '''
      CREATE TRIGGER insert_display_trigger
      AFTER INSERT ON $tableLicense
      WHEN NEW.key IN ('FRE-HUB-OPENED-VISITS', 'FRE-HUB-VEHICLES', 'FRE-HUB-RESIDENTS')
      BEGIN
        UPDATE $tableLicense
        SET display =
          CASE
            WHEN (SELECT COUNT(*) FROM $tableLicense WHERE key IN ('FRE-HUB-OPENED-VISITS', 'FRE-HUB-VEHICLES', 'FRE-HUB-RESIDENTS') AND display = 'VISIVEL') > 0
            THEN 'VISIVEL'
            ELSE 'INVISIVEL'
          END
        WHERE key = 'FRE-HUB-ABOUT-PROPERTY';
      END;
    ''';

  static String get dropInsertDisplayTrigger =>
      'DROP TRIGGER IF EXISTS insert_display_trigger;';

  static String get updatePeopleDisplayTrigger => '''
      CREATE TRIGGER update_people_display_trigger
      AFTER UPDATE ON $tableLicense
      WHEN NEW.key IN ('FRE-HUB-OPENED-VISITS', 'FRE-HUB-VEHICLES', 'FRE-HUB-RESIDENTS')
      BEGIN
        UPDATE $tableLicense
        SET display =
          CASE
            WHEN (SELECT COUNT(*) FROM $tableLicense WHERE key IN ('FRE-HUB-OPENED-VISITS', 'FRE-HUB-VEHICLES', 'FRE-HUB-RESIDENTS') AND display = 'VISIVEL') > 0
            THEN 'VISIVEL'
            ELSE 'INVISIVEL'
          END
        WHERE key = 'FRE-HUB-ABOUT-PROPERTY';
      END;
    ''';

  static String get dropPeopleDisplayTrigger =>
      'DROP TRIGGER IF EXISTS update_people_display_trigger;';
}
