import type { ISimpleLocalConfig, ISimpleLocalIntegrationProfile, ISimpleLocalSnapshot, TSimpleLocalRawData } from '../../core/index.js';

export const mobileAppDomain = "mobile_app";
export const mobileAppDefaultName = "Mobile App";
export const mobileAppDefaultPort = 8123;
export const mobileAppDefaultTimeoutMs = 10000;

export type TMobileAppSensorType = 'sensor' | 'binary_sensor';
export type TMobileAppWebhookType =
  | 'call_service'
  | 'conversation_process'
  | 'enable_encryption'
  | 'fire_event'
  | 'get_config'
  | 'get_zones'
  | 'register_sensor'
  | 'render_template'
  | 'scan_tag'
  | 'stream_camera'
  | 'update_location'
  | 'update_registration'
  | 'update_sensor_states';

export interface IMobileAppRegistrationRequest {
  device_id: string;
  app_id: string;
  app_name: string;
  app_version: string;
  device_name: string;
  manufacturer: string;
  model: string;
  os_name: string;
  os_version?: string;
  supports_encryption: boolean;
  app_data?: Record<string, unknown>;
}

export interface IMobileAppRegistrationResponse {
  cloudhook_url?: string | null;
  remote_ui_url?: string | null;
  secret?: string;
  webhook_id: string;
}

export interface IMobileAppWebhookPayload<TData = unknown> {
  type: TMobileAppWebhookType | string;
  data?: TData;
}

export interface IMobileAppSensorState {
  attributes?: Record<string, unknown>;
  device_class?: string | null;
  disabled?: boolean;
  entity_category?: string | null;
  icon?: string | null;
  name?: string;
  state: boolean | number | string | null;
  state_class?: string | null;
  type: TMobileAppSensorType;
  unique_id: string;
  unit_of_measurement?: string | null;
}

export type TMobileAppRawData = TSimpleLocalRawData;
export interface IMobileAppSnapshot extends ISimpleLocalSnapshot {}
export interface IMobileAppConfig extends ISimpleLocalConfig {
  url?: string;
  baseUrl?: string;
  accessToken?: string;
  macAddress?: string;
  deviceId?: string;
  appId?: string;
  appName?: string;
  appVersion?: string;
  deviceName?: string;
  manufacturer?: string;
  model?: string;
  osName?: string;
  osVersion?: string;
  supportsEncryption?: boolean;
  appData?: Record<string, unknown>;
  app_data?: Record<string, unknown>;
  registration?: IMobileAppRegistrationResponse;
  webhookId?: string;
  webhookUrl?: string;
  cloudhookUrl?: string;
  remoteUiUrl?: string;
  secret?: string;
}
export interface IHomeAssistantMobileAppConfig extends IMobileAppConfig {}

export const mobileAppProfile: ISimpleLocalIntegrationProfile = {
  domain: mobileAppDomain,
  displayName: "Mobile App",
  manufacturer: "Mobile App",
  model: "Mobile App local integration",
  defaultName: mobileAppDefaultName,
  defaultPort: mobileAppDefaultPort,
  defaultHttpPath: '/api/mobile_app/registrations',
  defaultProtocol: 'http',
  status: "read-only-runtime",
  platforms: [
    "binary_sensor",
    "sensor",
  ],
  serviceDomains: [],
  controlServices: [],
  discoverySources: [
    'manual',
    'custom',
  ],
  discoveryKeywords: [
    "mobile_app",
    "Mobile App",
    "Mobile",
    "App",
  ],
  metadata: {
    "source": "home-assistant/core",
    "upstreamPath": "homeassistant/components/mobile_app",
    "upstreamDomain": "mobile_app",
    "integrationType": "device",
    "iotClass": "local_push",
    "qualityScale": "internal",
    "requirements": [
      "PyNaCl==1.6.2",
    ],
    "dependencies": [
      "http",
      "intent",
      "person",
      "tag",
      "webhook",
      "websocket_api",
    ],
    "afterDependencies": [
      "cloud",
      "camera",
      "conversation",
      "notify",
    ],
    "codeowners": [
      "@home-assistant/core",
    ],
    "configFlow": true,
    "documentation": "https://www.home-assistant.io/integrations/mobile_app",
    "loggers": [
      "nacl",
    ],
    "runtime": {
      "type": "read-only-runtime",
      "services": [
        "snapshot",
        "status",
        "refresh",
      ],
      "platforms": [
        "binary_sensor",
        "sensor",
      ],
      "controls": false,
    },
    "localApi": {
      "implemented": [
        "local Home Assistant HTTP registration via POST /api/mobile_app/registrations with bearer token authentication",
        "local unauthenticated mobile_app webhook POSTs to /api/webhook/<webhook_id> for unencrypted registrations",
        "manual local setup for Mobile App endpoints, snapshots, rawData, snapshotProvider, and injected native clients",
        "runtime mapping for Mobile App device snapshots and Home Assistant platform metadata",
        "safe command delegation to injected client.execute or commandExecutor only",
      ],
      "explicitUnsupported": [
        "encrypted mobile_app webhook payloads because Home Assistant uses PyNaCl/Sodium and this package cannot add a Sodium dependency here",
        "OAuth2/IndieAuth browser pairing; this client requires an already issued Home Assistant long-lived access token",
        "Home Assistant entity registry migrations and repair flows",
        "claiming encrypted webhook, cloud push channel, websocket push, or notification delivery success without a real HTTP response",
      ],
    },
  },
};
