import { sdk } from '../sdk'
import { configFile } from '../file-models/config.json'

const { InputSpec, Value } = sdk

// Anonymous-trial allowance per first-time visitor. Multi-tenant mode
// only. When a visitor lands on the public Recaps URL without a session
// cookie and submits a YouTube URL, the server issues them a
// recap_anon_trial cookie with this many free summaries — no email,
// no signup, no friction. After they're spent, the UI nudges them to
// create an account for more credits.
//
// Trial summaries draw from the OPERATOR's relay credit pool, so this
// number times the visitor volume sets the floor on your sample-cost
// exposure. Tune downward if you see abuse, upward if you want a more
// generous activation funnel. Set to 0 to disable trials entirely
// (visitors immediately hit the sign-up gate).
//
// Defaults to 1 — enough to demo the value prop, tight enough that
// scripted-signup abuse doesn't drain the pool fast.
const inputSpec = InputSpec.of({
  credits: Value.number({
    name: 'Trial credits per anonymous visitor',
    description:
      'How many free summaries an unauthenticated visitor gets before being asked to sign up. Charged against your relay credit pool. Set to 0 to disable trials (immediate sign-up gate).',
    required: true,
    default: 1,
    integer: true,
    min: 0,
    max: 5,
  }),
})

export const setTrialCreditsPerVisitor = sdk.Action.withInput(
  'set-trial-credits-per-visitor',

  async ({ effects }) => ({
    name: 'Set Trial Credits per Visitor',
    description:
      "How many free summaries anonymous visitors get on your multi-tenant Recaps before being prompted to sign up. Default: 1. Charged against your relay credit pool.",
    warning: null,
    allowedStatuses: 'any',
    group: 'Multi-Tenant',
    visibility: 'enabled',
  }),

  inputSpec,

  async ({ effects }) => {
    const config = await configFile.read().once()
    return { credits: config?.trial_credits_per_visitor ?? 1 }
  },

  async ({ effects, input }) => {
    await configFile.merge(effects, {
      trial_credits_per_visitor: input.credits,
    })
    return null
  },
)
