/**
 * Claiming an imported historical identity (Nash, 2026-09-25, Synchron import
 * task §19–§25, §41–§44).
 *
 * An imported person is a `profiles` row with claim_status = 'unclaimed' and
 * no account. When someone registers with that email and PROVES it, the
 * profile, its company relationships, its roles and its historical trip
 * participations connect to the account. Nothing connects before proof, and
 * proof of an email is not proof of Company Admin (§48).
 *
 * Pure rules; the writes live in src/lib/data/historical-profiles.ts.
 */

export type ClaimStatus = 'unclaimed' | 'claim_candidate' | 'claimed' | 'manual_review' | 'conflict'

export interface ClaimableProfile {
  id: string
  claim_status: ClaimStatus | null
  claimed_user_id?: string | null
}

export type ClaimDecision =
  | { action: 'none'; reason: 'no_historical_profile' | 'not_imported' }
  | { action: 'wait'; reason: 'email_not_verified' }
  | { action: 'hold'; reason: 'manual_review' | 'conflict' }
  | { action: 'already'; reason: 'claimed_by_this_account' }
  | { action: 'conflict'; reason: 'claimed_by_another_account' }
  /** The account id equals the imported profile id: rows already point at the right person. */
  | { action: 'claim' }
  /** An existing account with a different id: relationships must be re-pointed to it (§17, §53). */
  | { action: 'claim_repoint'; fromProfileId: string }

/** Decide what a verified (or not) sign-in does with a matching historical profile. */
export function decideHistoricalClaim(params: { profile: ClaimableProfile | null; userId: string; emailVerified: boolean }): ClaimDecision {
  const { profile, userId, emailVerified } = params
  if (!profile) return { action: 'none', reason: 'no_historical_profile' }
  if (!profile.claim_status) return { action: 'none', reason: 'not_imported' }
  if (profile.claim_status === 'claimed') {
    const owner = profile.claimed_user_id ?? profile.id
    return owner === userId ? { action: 'already', reason: 'claimed_by_this_account' } : { action: 'conflict', reason: 'claimed_by_another_account' }
  }
  if (profile.claim_status === 'manual_review' || profile.claim_status === 'conflict') return { action: 'hold', reason: profile.claim_status }
  // §20: the email must be proved first. This is the rule the backend enforces; the UI only reflects it.
  if (!emailVerified) return { action: 'wait', reason: 'email_not_verified' }
  return profile.id === userId ? { action: 'claim' } : { action: 'claim_repoint', fromProfileId: profile.id }
}

/**
 * §43 — whether a participant row on an IMPORTED trip lets this person read
 * it. Only a row the claim turned live grants access; verification alone
 * grants nothing, and an unverified account never sees an imported row.
 */
export function historicalTripAccess(params: { participantStatus: string | null | undefined; emailVerified: boolean }): boolean {
  if (!params.emailVerified) return false
  return params.participantStatus === 'active' || params.participantStatus === 'invited'
}

/** After a claim, which membership / role statuses become live (§22, §47). */
export const CLAIM_TRANSITIONS = {
  membership: { from: 'historical_pending_confirmation', to: 'approved' },
  role: { from: 'pending', to: 'active' },
  participant: { from: 'imported', to: 'active' },
} as const

/** §41 — shown when a registration email matches an unclaimed profile. Says nothing about which trips. */
export const HISTORICAL_FOUND_MESSAGE =
  'We found previous activity associated with this email. Verify your email to connect your historical trips and company relationships.'
/** §42 — shown once the claim has happened. */
export const HISTORICAL_CONNECTED_MESSAGE = 'Your historical HeavyHaul/Synchron activity has been connected.'
