import { z } from 'zod'
import { DescriptionSchema, LabelSchema } from './shared'

// ---------------------------------------------------------------------------
// Business hours — simplest shape that captures weekday ranges.
// Each day of the week is optional; when present it holds open/close times
// in "HH:MM" 24-hour format. An empty object (default) means "not configured".
// ---------------------------------------------------------------------------

export const BusinessHoursDaySchema = z.object({
  open: z
    .string()
    .trim()
    .regex(/^\d{2}:\d{2}$/, 'Expected HH:MM format'),
  close: z
    .string()
    .trim()
    .regex(/^\d{2}:\d{2}$/, 'Expected HH:MM format')
})

export const BusinessHoursSchema = z
  .object({
    monday: BusinessHoursDaySchema.optional(),
    tuesday: BusinessHoursDaySchema.optional(),
    wednesday: BusinessHoursDaySchema.optional(),
    thursday: BusinessHoursDaySchema.optional(),
    friday: BusinessHoursDaySchema.optional(),
    saturday: BusinessHoursDaySchema.optional(),
    sunday: BusinessHoursDaySchema.optional()
  })
  .default({})

// ---------------------------------------------------------------------------
// Identity domain schema — legal identity, mission/vision, industry anchors,
// and temporal/geographic context. Distinct from branding (display identity).
// ---------------------------------------------------------------------------

export const IdentityDomainSchema = z
  .object({
    /** Why the organization exists — one or two plain-language sentences. */
    mission: z.string().trim().max(1000).default(''),
    /** Long-term direction the organization is moving toward. */
    vision: z.string().trim().max(1000).default(''),
    /** Legal registered name of the entity. */
    legalName: z.string().trim().max(200).default(''),
    /**
     * Type of legal entity (e.g. "LLC", "Corporation", "Sole Proprietor",
     * "Non-profit"). Free-form string so it covers any jurisdiction.
     */
    entityType: z.string().trim().max(100).default(''),
    /**
     * Primary jurisdiction of registration or operation
     * (e.g. "United States – Delaware", "Canada – Ontario").
     */
    jurisdiction: z.string().trim().max(200).default(''),
    /**
     * Industry category — broad classification (e.g. "Marketing Agency",
     * "Software / SaaS", "Professional Services").
     */
    industryCategory: z.string().trim().max(200).default(''),
    /**
     * Geographic focus — where the organization primarily operates or serves
     * (e.g. "North America", "Global", "Southeast Asia").
     */
    geographicFocus: z.string().trim().max(200).default(''),
    /**
     * IANA timezone identifier for the organization's primary operating timezone
     * (e.g. "America/Los_Angeles", "Europe/London", "UTC").
     */
    timeZone: z.string().trim().max(100).default('UTC'),
    /** Typical operating hours per day of week. Empty object means not configured. */
    businessHours: BusinessHoursSchema,
    /**
     * Long-form markdown capturing client context, problem narrative, and domain
     * background. Populated by /setup; surfaced to agents as organizational context.
     * Optional — many projects have no external client.
     */
    clientBrief: z.string().trim().default(''),
    /**
     * Display name of the organization as shown to end users.
     * Recommended placement for display naming — prefer this over the deprecated
     * `branding.organizationName`. Falls back to `branding.organizationName` for
     * legacy tenants that have not yet migrated.
     */
    organizationName: LabelSchema.optional(),
    /**
     * Display name of the primary product or platform.
     * Recommended placement for display naming — prefer this over the deprecated
     * `branding.productName`. Falls back to `branding.productName` for legacy tenants.
     */
    productName: LabelSchema.optional(),
    /**
     * Short abbreviated name used in space-constrained UI surfaces (max 40 chars).
     * Recommended placement for display naming — prefer this over the deprecated
     * `branding.shortName`. Falls back to `branding.shortName` for legacy tenants.
     */
    shortName: z.string().trim().min(1).max(40).optional(),
    /**
     * Plain-language description of the organization for display and discovery.
     * Recommended placement for display naming — prefer this over the deprecated
     * `branding.description`. Falls back to `branding.description` for legacy tenants.
     */
    description: DescriptionSchema.optional()
  })
  .passthrough()

// ---------------------------------------------------------------------------
// Seed — placeholder defaults; external adapters override per organization.
// ---------------------------------------------------------------------------

export const DEFAULT_ORGANIZATION_MODEL_IDENTITY: z.infer<typeof IdentityDomainSchema> = {
  mission: '',
  vision: '',
  legalName: '',
  entityType: '',
  jurisdiction: '',
  industryCategory: '',
  geographicFocus: '',
  timeZone: 'UTC',
  businessHours: {},
  clientBrief: ''
}
