import type { ApiLLMSettings, ApiSearchSettings } from "@/lib/contacts/discovery-types"

export type AuthentikSettings = {
  enabled: boolean
  api_url: string
  slug: string
  client_id: string
  enforce_sso: boolean
  allow_password_fallback: boolean
  default_groups: string
}

export type IdentityProviderType = "oauth" | "saml" | "ldap"

export type OAuthProviderPreset =
  | "google"
  | "github"
  | "linkedin"
  | "microsoft"
  | "custom"

export type IdentityProviderOAuth = {
  provider: OAuthProviderPreset
  client_id: string
  client_secret: string
  scopes: string
  authorization_url?: string
  token_url?: string
  profile_url?: string
}

export type IdentityProviderSAML = {
  metadata_url?: string
  metadata_xml?: string
  entity_id?: string
  sso_url?: string
  slo_url?: string
  signing_cert?: string
}

export type IdentityProviderLDAP = {
  server_uri: string
  bind_dn: string
  bind_password: string
  base_dn: string
  user_filter?: string
  start_tls: boolean
  sync_users?: boolean
}

export type IdentityProviderSyncStatus = "pending" | "synced" | "error"

export type IdentityProvider = {
  id: string
  name: string
  slug: string
  type: IdentityProviderType
  enabled: boolean
  authentik_pk?: number
  sync_status: IdentityProviderSyncStatus
  sync_error?: string
  last_synced_at?: string
  allowed_email_domains: string[]
  allowed_identities: string[]
  allowed_organizations: string[]
  default_groups: string[]
  oauth?: IdentityProviderOAuth
  saml?: IdentityProviderSAML
  ldap?: IdentityProviderLDAP
}

export type IdentityProvidersPolicy = {
  allow_self_enrollment: boolean
  default_login_source?: string
  providers: IdentityProvider[]
}

export type TwoFactorPolicy = {
  required_for_all: boolean
  required_for_admins: boolean
  allowed_methods: ("totp" | "webauthn")[]
  grace_period_days: number
  remember_device_days: number
}

export type OrgStorageQuotas = {
  default_mail_gib: number
  default_drive_gib: number
  default_photos_gib: number
  warn_threshold_pct: number
}

export type UsageQuotaDefaults = {
  llm_daily_cost_limit_eur: number
  llm_monthly_cost_limit_eur: number
  llm_cost_warn_threshold_pct: number
  /** @deprecated legacy */
  llm_requests_per_day: number
  llm_tokens_per_month: number
  search_requests_per_day: number
  max_api_tokens_per_user: number
  max_webhooks_per_user: number
}

export type DriveMountOAuthProvider = "google" | "dropbox" | "microsoft"

export type DriveMountOAuthProviderSettings = {
  enabled: boolean
  client_id: string
  client_secret: string
}

export type DriveMountOAuthSettings = {
  redirect_uri: string
  google: DriveMountOAuthProviderSettings
  dropbox: DriveMountOAuthProviderSettings
  microsoft: DriveMountOAuthProviderSettings
}

export type FilePolicySettings = {
  max_upload_mib: number
  allowed_extensions: string
  block_executable: boolean
  external_sharing: "disabled" | "authenticated" | "public_link"
  default_link_expiry_days: number
  virus_scan_enabled: boolean
  virustotal_api_key: string
  retention_trash_days: number
  mount_oauth: DriveMountOAuthSettings
}

export type OrgLLMSettings = ApiLLMSettings & {
  enforce_org_providers: boolean
  allow_user_override: boolean
}

export type OrgSearchEngine = "postgres" | "meilisearch" | "typesense"

export type OrgSearchSettings = {
  suite_engine: OrgSearchEngine
  meilisearch_url: string
  meilisearch_api_key: string
  typesense_url: string
  typesense_api_key: string
  web_search: ApiSearchSettings
  enforce_org_search: boolean
}

export type Administrator = {
  id: string
  email: string
  name: string
  external_id?: string
  scopes: ("read" | "write")[]
  added_at: string
}

export type NextcloudSettings = {
  enabled: boolean
  base_url: string
  admin_user: string
  admin_password: string
  drive_enabled: boolean
  calendar_enabled: boolean
  contacts_enabled: boolean
  talk_enabled: boolean
}

export type MailingSettings = {
  enabled: boolean
  smtp_host: string
  smtp_port: number
  smtp_user: string
  smtp_password: string
  from_email: string
  from_name: string
  tls_mode: "starttls" | "ssl" | "none"
  reply_to?: string
}

export type OnlyOfficeSettings = {
  enabled: boolean
  document_server_url: string
  jwt_secret: string
  jwt_header: string
}

export type RichTextSettings = {
  enabled: boolean
  storage_mode: "sidecar" | "overwrite"
  export_mirror_format: "" | "docx"
  hocuspocus_url: string
}

export type AiModelCatalogEntry = {
  model_id: string
  label: string
  enabled: boolean
}

export type AiAssistantSettings = {
  enabled: boolean
  openwebui_internal_url: string
  public_path: string
  embed_default_temporary: boolean
  default_model: string
  enabled_tools: string[]
  chat_sync_enabled: boolean
  chat_nc_path: string
  models: AiModelCatalogEntry[]
}

export type {
  MeetOrgPolicySettings,
  MeetPostActionsSettings,
  MeetTranscriptionMode,
  MeetTranscriptionEngine,
} from "@/lib/meet/meet-settings-types"

export type AgendaOrgPolicySettings = {
  default_theme_mode: "light" | "dark" | "system"
  enforce_org_theme: boolean
  default_video_provider: "ultimeet" | "google_meet" | "zoom" | "teams" | "jitsi" | "none"
  enforce_org_video_provider: boolean
  video_provider_api_keys: Partial<
    Record<"ultimeet" | "google_meet" | "zoom" | "teams" | "jitsi" | "none", string>
  >
}

export type PluginEntry = {
  id: string
  name: string
  description: string
  enabled: boolean
  version: string
}

export type IntegrationEntry = {
  id: string
  name: string
  description: string
  enabled: boolean
  configured: boolean
  href?: string
}

export type OrgSettingsState = {
  authentik: AuthentikSettings
  identityProviders: IdentityProvidersPolicy
  twoFactor: TwoFactorPolicy
  storageQuotas: OrgStorageQuotas
  usageQuotas: UsageQuotaDefaults
  filePolicies: FilePolicySettings
  llm: OrgLLMSettings
  search: OrgSearchSettings
  administrators: Administrator[]
  nextcloud: NextcloudSettings
  mailing: MailingSettings
  onlyoffice: OnlyOfficeSettings
  richtext: RichTextSettings
  aiAssistant: AiAssistantSettings
  agenda: AgendaOrgPolicySettings
  meet: MeetOrgPolicySettings
  plugins: PluginEntry[]
  integrations: IntegrationEntry[]
}
