module.exports = {
  name: 'self_hosted_licenses',
  up: async (client) => {
    await client.query(`
      CREATE TABLE IF NOT EXISTS self_hosted_licenses (
        id                            SERIAL PRIMARY KEY,
        license_key                   VARCHAR(64) UNIQUE NOT NULL,
        user_id                       INTEGER REFERENCES users(id),
        plan_tier                     VARCHAR(32)  NOT NULL DEFAULT 'starter',
        max_connections               INTEGER      NOT NULL DEFAULT 5,
        max_health_checks_per_month   INTEGER      NOT NULL DEFAULT 100,
        status                        VARCHAR(16)  NOT NULL DEFAULT 'active', -- active, suspended, expired
        issued_at                     TIMESTAMPTZ  DEFAULT NOW(),
        expires_at                    TIMESTAMPTZ,
        last_validated_at             TIMESTAMPTZ,
        instance_id                   VARCHAR(128), -- set on first validation, locked thereafter
        created_at                    TIMESTAMPTZ  DEFAULT NOW()
      )
    `);
    await client.query(`
      CREATE INDEX IF NOT EXISTS idx_licenses_key ON self_hosted_licenses(license_key)
    `);
    await client.query(`
      CREATE INDEX IF NOT EXISTS idx_licenses_user ON self_hosted_licenses(user_id)
    `);
  },
};
