import { DiscoveryDescriptor } from '../../core/classes.discoverydescriptor.js';
import type { IDiscoveryCandidate, IDiscoveryMatch, IDiscoveryMatcher, IDiscoveryValidator } from '../../core/types.js';

export interface IWolfSmartsetManualEntry {
  host?: string;
  username?: string;
  metadata?: Record<string, unknown>;
}

export class WolfSmartsetManualMatcher implements IDiscoveryMatcher<IWolfSmartsetManualEntry> {
  public id = 'wolf-smartset-manual-match';
  public source = 'manual' as const;
  public description = 'Recognize manual Wolf Smartset setup entries.';

  public async matches(inputArg: IWolfSmartsetManualEntry): Promise<IDiscoveryMatch> {
    if (!inputArg.host && !inputArg.username && !inputArg.metadata?.wolfSmartset) {
      return {
        matched: false,
        confidence: 'low',
        reason: 'No Wolf Smartset manual setup hints found.',
      };
    }
    return {
      matched: true,
      confidence: inputArg.host ? 'high' : 'medium',
      reason: 'Manual entry can start Wolf Smartset setup.',
      candidate: {
        source: 'manual',
        integrationDomain: 'wolf_smartset',
        host: inputArg.host,
        manufacturer: 'Wolf',
        metadata: inputArg.metadata,
      },
    };
  }
}

export class WolfSmartsetValidator implements IDiscoveryValidator {
  public id = 'wolf-smartset-validator';
  public description = 'Validate Wolf Smartset candidate metadata.';

  public async validate(candidateArg: IDiscoveryCandidate): Promise<IDiscoveryMatch> {
    const matched = candidateArg.integrationDomain === 'wolf_smartset' || candidateArg.manufacturer === 'Wolf';
    return {
      matched,
      confidence: matched ? 'medium' : 'low',
      reason: matched ? 'Candidate has Wolf Smartset metadata.' : 'Candidate is not Wolf Smartset.',
      candidate: matched ? candidateArg : undefined,
      normalizedDeviceId: candidateArg.id,
    };
  }
}

export const createWolfSmartsetDiscoveryDescriptor = (): DiscoveryDescriptor => {
  return new DiscoveryDescriptor({
    integrationDomain: 'wolf_smartset',
    displayName: 'Wolf Smartset',
  })
    .addMatcher(new WolfSmartsetManualMatcher())
    .addValidator(new WolfSmartsetValidator());
};
