export type ChatMessage = {
  author: string;
  text: string;
  timestamp: string;
};

export type AvatarAction =
  | { type: 'talk:start' }
  | { type: 'talk:stop' }
  | { type: 'emotion'; value: 'neutral' | 'happy' | 'excited' | 'curious' }
  | { type: 'gesture'; value: 'nod' | 'wave' | 'tilt' };

export type AgentResponse = {
  reply: string;
  actions: AvatarAction[];
  audio?: {
    provider: 'gemini-live' | 'stub';
    text: string;
  };
};

export interface ResponseProvider {
  generateReply(message: ChatMessage): Promise<string>;
}

function planAvatarActions(reply: string): AvatarAction[] {
  const lower = reply.toLowerCase();
  return [
    { type: 'talk:start' },
    { type: 'emotion', value: lower.includes('!') ? 'excited' : lower.includes('?') ? 'curious' : 'happy' },
    { type: 'gesture', value: lower.includes('?') ? 'tilt' : 'nod' },
    { type: 'talk:stop' }
  ];
}

export class StubResponseProvider implements ResponseProvider {
  async generateReply(message: ChatMessage): Promise<string> {
    return `Hey ${message.author} — I heard: ${message.text}`;
  }
}

export class CharacterRuntime {
  constructor(private readonly provider: ResponseProvider) {}

  async handleMessage(message: ChatMessage): Promise<AgentResponse> {
    const reply = await this.provider.generateReply(message);
    const actions = planAvatarActions(reply);
    return {
      reply,
      actions,
      audio: {
        provider: 'stub',
        text: reply
      }
    };
  }
}

