import type { IConfigFlow, IConfigFlowContext, IConfigFlowStep, IDiscoveryCandidate } from '../../core/types.js';
import type { INanoleafConfig } from './nanoleaf.types.js';

const DEFAULT_NANOLEAF_PORT = 16021;

export class NanoleafConfigFlow implements IConfigFlow<INanoleafConfig> {
  public async start(candidateArg: IDiscoveryCandidate, contextArg: IConfigFlowContext): Promise<IConfigFlowStep<INanoleafConfig>> {
    void contextArg;
    return {
      kind: 'form',
      title: 'Connect Nanoleaf',
      description: 'Configure the local Nanoleaf HTTP endpoint with an existing auth token from the controller.',
      fields: [
        { name: 'host', label: 'Host', type: 'text', required: true },
        { name: 'port', label: 'Port', type: 'number', required: true },
        { name: 'authToken', label: 'Auth token', type: 'password', required: true },
      ],
      submit: async (valuesArg) => ({
        kind: 'done',
        title: 'Nanoleaf configured',
        config: {
          host: String(valuesArg.host || candidateArg.host || ''),
          port: typeof valuesArg.port === 'number' ? valuesArg.port : candidateArg.port || DEFAULT_NANOLEAF_PORT,
          authToken: String(valuesArg.authToken || ''),
        },
      }),
    };
  }
}
