import type { IConfigFlow, IConfigFlowContext, IConfigFlowStep, IDiscoveryCandidate } from '../../core/types.js';
import { FritzMapper } from './fritz.mapper.js';
import type { IFritzConfig, IFritzSnapshot } from './fritz.types.js';
import { fritzDefaultConsiderHomeSeconds, fritzDefaultHost, fritzDefaultSsl } from './fritz.types.js';

export class FritzConfigFlow implements IConfigFlow<IFritzConfig> {
  public async start(candidateArg: IDiscoveryCandidate, contextArg: IConfigFlowContext): Promise<IConfigFlowStep<IFritzConfig>> {
    void contextArg;
    const metadata = candidateArg.metadata || {};
    const ssl = this.booleanValue(metadata.ssl) ?? fritzDefaultSsl;
    return {
      kind: 'form',
      title: 'Connect FRITZ!Box Tools',
      description: 'Provide the local FRITZ!Box endpoint. Snapshot/manual data is supported directly; live TR-064/HTTP success is not assumed without an injected native client or command executor.',
      fields: [
        { name: 'host', label: candidateArg.host ? `Host (${candidateArg.host})` : `Host (${fritzDefaultHost})`, type: 'text', required: true },
        { name: 'port', label: `Port (${candidateArg.port || FritzMapper.defaultPort(ssl)})`, type: 'number' },
        { name: 'username', label: 'Username', type: 'text' },
        { name: 'password', label: 'Password', type: 'password' },
        { name: 'ssl', label: 'Use SSL', type: 'boolean' },
        { name: 'featureDeviceTracking', label: 'Enable network device tracking', type: 'boolean' },
        { name: 'oldDiscovery', label: 'Use old hosts discovery method', type: 'boolean' },
        { name: 'considerHomeSeconds', label: `Seconds to consider a device home (${fritzDefaultConsiderHomeSeconds})`, type: 'number' },
        { name: 'snapshotJson', label: 'Snapshot JSON', type: 'text' },
      ],
      submit: async (valuesArg) => this.submit(candidateArg, valuesArg),
    };
  }

  private async submit(candidateArg: IDiscoveryCandidate, valuesArg: Record<string, unknown>): Promise<IConfigFlowStep<IFritzConfig>> {
    const snapshot = this.snapshotFromInput(valuesArg.snapshotJson || candidateArg.metadata?.snapshot);
    if (snapshot instanceof Error) {
      return { kind: 'error', title: 'Invalid FRITZ!Box snapshot', error: snapshot.message };
    }

    const ssl = this.booleanValue(valuesArg.ssl) ?? this.booleanValue(candidateArg.metadata?.ssl) ?? snapshot?.router.ssl ?? fritzDefaultSsl;
    const host = this.stringValue(valuesArg.host) || candidateArg.host || snapshot?.router.host || (!snapshot ? fritzDefaultHost : undefined);
    if (!host && !snapshot) {
      return { kind: 'error', title: 'FRITZ!Box setup failed', error: 'FRITZ!Box setup requires a host or snapshot JSON.' };
    }

    const port = this.numberValue(valuesArg.port) || candidateArg.port || snapshot?.router.port || (host ? FritzMapper.defaultPort(ssl) : undefined);
    const config: IFritzConfig = {
      host,
      port,
      ssl,
      username: this.stringValue(valuesArg.username),
      password: this.stringValue(valuesArg.password),
      featureDeviceTracking: this.booleanValue(valuesArg.featureDeviceTracking) ?? true,
      oldDiscovery: this.booleanValue(valuesArg.oldDiscovery) ?? false,
      considerHomeSeconds: this.numberValue(valuesArg.considerHomeSeconds) || fritzDefaultConsiderHomeSeconds,
      uniqueId: candidateArg.id || snapshot?.router.serialNumber || snapshot?.router.macAddress,
      name: candidateArg.name || snapshot?.router.name || snapshot?.router.model || host,
      model: candidateArg.model || snapshot?.router.model,
      snapshot,
      metadata: {
        discoverySource: candidateArg.source,
        discoveryMetadata: candidateArg.metadata,
        upstreamSupportsSsdp: true,
        upstreamSupportsZeroconf: false,
        liveTr064Implemented: false,
      },
    };

    return {
      kind: 'done',
      title: 'FRITZ!Box Tools configured',
      config,
    };
  }

  private snapshotFromInput(valueArg: unknown): IFritzSnapshot | undefined | Error {
    if (valueArg && typeof valueArg === 'object') {
      return valueArg as IFritzSnapshot;
    }
    const text = this.stringValue(valueArg);
    if (!text) {
      return undefined;
    }
    try {
      const parsed = JSON.parse(text) as IFritzSnapshot;
      if (!parsed || typeof parsed !== 'object' || !parsed.router) {
        return new Error('Snapshot JSON must include a router object.');
      }
      return parsed;
    } catch (errorArg) {
      return errorArg instanceof Error ? errorArg : new Error(String(errorArg));
    }
  }

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

  private numberValue(valueArg: unknown): number | undefined {
    if (typeof valueArg === 'number' && Number.isFinite(valueArg) && valueArg >= 0) {
      return Math.round(valueArg);
    }
    if (typeof valueArg === 'string' && valueArg.trim()) {
      const parsed = Number(valueArg);
      return Number.isFinite(parsed) && parsed >= 0 ? Math.round(parsed) : undefined;
    }
    return undefined;
  }

  private booleanValue(valueArg: unknown): boolean | undefined {
    if (typeof valueArg === 'boolean') {
      return valueArg;
    }
    if (typeof valueArg === 'string') {
      if (['true', '1', 'yes', 'on'].includes(valueArg.toLowerCase())) {
        return true;
      }
      if (['false', '0', 'no', 'off'].includes(valueArg.toLowerCase())) {
        return false;
      }
    }
    return undefined;
  }
}
