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

const { InputSpec, Value } = sdk

// The Recaps public URL is the ClearNet URL where users access the app
// — typically a domain you've pointed at Start Tunnel. Magic-link
// sign-in emails interpolate this URL into the verification link:
//
//   Click here to sign in: https://recap.example.com/auth/verify?token=...
//
// Without this set, magic-link emails contain a link to localhost or
// to the StartOS internal hostname, neither of which work for a user
// reading the email on a different device.
//
// Only meaningful when recap_mode === 'multi'. In single mode the
// value is ignored — there's no magic-link flow.
const inputSpec = InputSpec.of({
  public_url: Value.text({
    name: 'Public URL',
    description:
      'Full URL where users reach your Recaps (e.g. https://recapapp.xyz). Include the https:// prefix. Used to build sign-in links in magic-link emails. Must be reachable from the public internet for users to receive a working link.',
    required: true,
    default: null,
    placeholder: 'https://recapapp.xyz',
    masked: false,
    minLength: 8,
    maxLength: 256,
  }),
})

export const setRecapPublicUrl = sdk.Action.withInput(
  'set-recap-public-url',

  async ({ effects }) => ({
    name: 'Set Recaps Public URL',
    description:
      'Set the ClearNet URL where users will access your Recaps. Used in magic-link sign-in emails. Required for multi-tenant mode.',
    warning: null,
    allowedStatuses: 'any',
    group: 'Multi-Tenant',
    visibility: 'enabled',
  }),

  inputSpec,

  async ({ effects }) => {
    const config = await configFile.read().once()
    return { public_url: config?.recap_public_url || undefined }
  },

  async ({ effects, input }) => {
    // Strip trailing slash for canonical form — the auth-link builder
    // concatenates "/auth/verify?token=..." without checking.
    const url = (input.public_url || '').trim().replace(/\/$/, '')
    await configFile.merge(effects, { recap_public_url: url })
    return null
  },
)
