import { SimpleLocalClient, SimpleLocalMapper, type IServiceCallRequest, type IServiceCallResult, type ISimpleLocalSnapshot, type TSimpleLocalRawData } from '../../core/index.js';
import type { IPicottsConfig } from './picotts.types.js';
import { picottsProfile } from './picotts.types.js';

export class PicottsClient extends SimpleLocalClient<IPicottsConfig> {
  private cachedSnapshot?: ISimpleLocalSnapshot;

  constructor(private readonly configArg: IPicottsConfig) {
    super(picottsProfile, configArg);
  }

  public override async getSnapshot(forceRefreshArg = false): Promise<ISimpleLocalSnapshot> {
    if (!forceRefreshArg && this.cachedSnapshot) {
      return SimpleLocalMapper.clone(this.cachedSnapshot);
    }

    if (this.configArg.client) {
      try {
        this.cachedSnapshot = await this.snapshotFromInjectedClient();
      } catch (errorArg) {
        this.cachedSnapshot = this.createOfflineSnapshot(errorMessage(errorArg));
      }
      return SimpleLocalMapper.clone(this.cachedSnapshot);
    }

    if (this.configArg.snapshotProvider) {
      try {
        const value = await this.configArg.snapshotProvider();
        if (value !== undefined) {
          this.cachedSnapshot = SimpleLocalMapper.toSnapshot({ profile: picottsProfile, config: this.configArg, rawData: value, source: 'provider', online: true });
          return SimpleLocalMapper.clone(this.cachedSnapshot);
        }
      } catch (errorArg) {
        this.cachedSnapshot = this.createOfflineSnapshot(errorMessage(errorArg));
        return SimpleLocalMapper.clone(this.cachedSnapshot);
      }
    }

    const rawData = this.configArg.rawData ?? this.configArg.rawText;
    if (rawData !== undefined || this.configArg.entities) {
      this.cachedSnapshot = SimpleLocalMapper.toSnapshot({ profile: picottsProfile, config: this.configArg, rawData, source: 'manual', online: this.configArg.online ?? true });
      return SimpleLocalMapper.clone(this.cachedSnapshot);
    }

    if (this.configArg.snapshot) {
      this.cachedSnapshot = SimpleLocalMapper.toSnapshot({ profile: picottsProfile, config: this.configArg, rawData: this.configArg.snapshot, source: 'snapshot', online: this.configArg.snapshot.online });
      return SimpleLocalMapper.clone(this.cachedSnapshot);
    }

    this.cachedSnapshot = this.createOfflineSnapshot('Pico TTS live synthesis is not implemented: Home Assistant requires the external pico2wave OS command via subprocess and a generated temporary WAV file, which is not an HTTP/TCP/TLS/file protocol available through existing integration dependencies.');
    return SimpleLocalMapper.clone(this.cachedSnapshot);
  }

  public override async refresh(): Promise<IServiceCallResult> {
    this.cachedSnapshot = undefined;
    const snapshot = await this.getSnapshot(true);
    const success = snapshot.online && !snapshot.error;
    return { success, error: success ? undefined : snapshot.error, data: snapshot };
  }

  public override async execute(requestArg: IServiceCallRequest): Promise<IServiceCallResult> {
    if (this.configArg.client?.execute || this.configArg.commandExecutor) {
      return super.execute(requestArg);
    }
    return {
      success: false,
      error: `Pico TTS service ${requestArg.service} requires an injected client.execute() or commandExecutor; native synthesis is blocked because HA shells out to pico2wave and reads its temporary WAV output.`,
    };
  }

  private async snapshotFromInjectedClient(): Promise<ISimpleLocalSnapshot> {
    const client = this.configArg.client;
    if (!client) {
      throw new Error('No Pico TTS client configured.');
    }
    if (client.getSnapshot) {
      return this.toSnapshot(await client.getSnapshot(), 'client');
    }
    if (client.getState) {
      return this.toSnapshot(await client.getState(), 'client');
    }
    if (client.getRawData) {
      return this.toSnapshot(await client.getRawData(), 'client');
    }
    throw new Error('Pico TTS client must expose getSnapshot(), getState(), or getRawData().');
  }

  private toSnapshot(valueArg: ISimpleLocalSnapshot | TSimpleLocalRawData, sourceArg: 'client' | 'provider'): ISimpleLocalSnapshot {
    return SimpleLocalMapper.toSnapshot({ profile: picottsProfile, config: this.configArg, rawData: valueArg, source: sourceArg, online: isSnapshot(valueArg) ? valueArg.online : true });
  }

  private createOfflineSnapshot(errorArg: string): ISimpleLocalSnapshot {
    return SimpleLocalMapper.toSnapshot({ profile: picottsProfile, config: this.configArg, online: false, source: 'runtime', error: errorArg });
  }
}

const isSnapshot = (valueArg: unknown): valueArg is ISimpleLocalSnapshot => {
  return Boolean(valueArg && typeof valueArg === 'object' && 'device' in valueArg && 'entities' in valueArg);
};

const errorMessage = (errorArg: unknown): string => {
  return errorArg instanceof Error ? errorArg.message : String(errorArg);
};
