/** @param {import('node-pg-migrate').MigrationBuilder} pgm */
exports.shorthands = undefined;

exports.up = (pgm) => {
  pgm.sql(`
    DO $$
    BEGIN
      IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'reporting_payment_lines_eur_only') THEN
        ALTER TABLE reporting_payment_lines
          ADD CONSTRAINT reporting_payment_lines_eur_only CHECK (currency = 'EUR');
      END IF;
      IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'reporting_payment_lines_valid_status') THEN
        ALTER TABLE reporting_payment_lines
          ADD CONSTRAINT reporting_payment_lines_valid_status
          CHECK (status IN ('payment', 'refund', 'partial_refund'));
      END IF;
    END
    $$ LANGUAGE plpgsql;
  `);
};

exports.down = (pgm) => {
  pgm.sql(`
    ALTER TABLE reporting_payment_lines
      DROP CONSTRAINT IF EXISTS reporting_payment_lines_valid_status,
      DROP CONSTRAINT IF EXISTS reporting_payment_lines_eur_only;
  `);
};
