import type { IConfigFlow, IConfigFlowContext, IConfigFlowStep, IDiscoveryCandidate } from '../../core/types.js';
import { harmonyDefaultDelaySecs, harmonyDisplayName, harmonyDomain, harmonyHubPort, harmonyManufacturer } from './harmony.constants.js';
import type { IHarmonyConfig } from './harmony.types.js';

export class HarmonyConfigFlow implements IConfigFlow<IHarmonyConfig> {
  public async start(candidateArg: IDiscoveryCandidate, contextArg: IConfigFlowContext): Promise<IConfigFlowStep<IHarmonyConfig>> {
    void contextArg;
    return {
      kind: 'form',
      title: 'Connect Logitech Harmony Hub',
      description: 'Configure a local Harmony Hub. Activity and remote commands require an injected client/executor unless a live local transport is supplied by the host application.',
      fields: [
        { name: 'host', label: 'Host', type: 'text', required: true },
        { name: 'port', label: 'Local API port', type: 'number' },
        { name: 'name', label: 'Name', type: 'text' },
        { name: 'remoteId', label: 'Remote ID', type: 'text' },
        { name: 'defaultActivity', label: 'Default activity', type: 'text' },
        { name: 'delaySecs', label: 'Delay between repeated commands', type: 'number' },
      ],
      submit: async (valuesArg) => {
        const host = this.stringValue(valuesArg.host) || candidateArg.host;
        if (!host) {
          return { kind: 'error', title: 'Harmony Hub configuration failed', error: 'Host is required.' };
        }
        const port = this.numberValue(valuesArg.port) || candidateArg.port || harmonyHubPort;
        const remoteId = this.stringValue(valuesArg.remoteId) || this.stringValue(candidateArg.metadata?.remoteId) || candidateArg.id;
        const name = this.stringValue(valuesArg.name) || candidateArg.name || harmonyDisplayName;
        const defaultActivity = this.stringValue(valuesArg.defaultActivity);
        const delaySecs = this.numberValue(valuesArg.delaySecs) ?? harmonyDefaultDelaySecs;
        return {
          kind: 'done',
          title: 'Harmony Hub configured',
          config: {
            host,
            port,
            name,
            remoteId,
            defaultActivity,
            delaySecs,
            manufacturer: candidateArg.manufacturer || harmonyManufacturer,
            model: candidateArg.model || 'Harmony Hub',
            deviceInfo: {
              id: remoteId || candidateArg.id,
              name,
              host,
              port,
              remoteId,
              manufacturer: candidateArg.manufacturer || harmonyManufacturer,
              model: candidateArg.model || 'Harmony Hub',
            },
            discovery: {
              source: candidateArg.source,
              metadata: candidateArg.metadata,
            },
          },
        };
      },
    };
  }

  private stringValue(valueArg: unknown): string | undefined {
    return typeof valueArg === 'string' && valueArg.trim() ? valueArg.trim() : undefined;
  }

  private numberValue(valueArg: unknown): number | undefined {
    return typeof valueArg === 'number' && Number.isFinite(valueArg) && valueArg > 0 ? valueArg : undefined;
  }
}
