exports.up = (pgm) => {
  pgm.createTable('trip_members', {
    id: { type: 'uuid', primaryKey: true, default: pgm.func('gen_random_uuid()') },
    trip_id: { type: 'uuid', notNull: true, references: 'trips(id)', onDelete: 'CASCADE' },
    user_id: { type: 'uuid', notNull: true, references: 'users(id)' },
    role: { type: 'text', notNull: true },
    status: { type: 'text', notNull: true },
    joined_at: { type: 'timestamptz' },
    created_at: { type: 'timestamptz', notNull: true, default: pgm.func('now()') },
    updated_at: { type: 'timestamptz', notNull: true, default: pgm.func('now()') },
  });
  pgm.addConstraint('trip_members', 'trip_members_trip_user_key', 'UNIQUE(trip_id, user_id)');
  pgm.addConstraint('trip_members', 'trip_members_role_check', "CHECK (role IN ('OWNER','MEMBER'))");
  pgm.addConstraint(
    'trip_members',
    'trip_members_status_check',
    "CHECK (status IN ('INVITED','ACTIVE','DECLINED'))",
  );
  pgm.createIndex('trip_members', 'trip_id');
  pgm.createIndex('trip_members', 'user_id');
};

exports.down = (pgm) => {
  pgm.dropTable('trip_members');
};
