interface SubtaskTemplate {
    keywords: string[];
    subtasks: string[];
}

const TEMPLATES: SubtaskTemplate[] = [
    {
        keywords: ['vacation', 'trip', 'travel', 'holiday', 'flight'],
        subtasks: [
            '- [ ] Research destination and dates',
            '- [ ] Book flights',
            '- [ ] Reserve accommodation',
            '- [ ] Check passport validity',
            '- [ ] Create packing list',
            '- [ ] Arrange transport to airport'
        ]
    },
    {
        keywords: ['feature', 'coding', 'code', 'bug', 'fix', 'implement', 'dev'],
        subtasks: [
            '- [ ] Analyze requirements',
            '- [ ] Design technical approach',
            '- [ ] Write implementation code',
            '- [ ] Write unit tests',
            '- [ ] Perform manual verification',
            '- [ ] Open Pull Request'
        ]
    },
    {
        keywords: ['groceries', 'shop', 'buy', 'store', 'market'],
        subtasks: [
            '- [ ] Check fridge/pantry inventory',
            '- [ ] Plan meals for the week',
            '- [ ] Write shopping list',
            '- [ ] Bring reusable bags',
            '- [ ] Go to store'
        ]
    },
    {
        keywords: ['meeting', 'call', 'sync', 'discussion', 'interview'],
        subtasks: [
            '- [ ] Prepare agenda',
            '- [ ] Review background materials',
            '- [ ] Set up video conference link',
            '- [ ] Take notes during meeting',
            '- [ ] Send follow-up action items'
        ]
    },
    {
        keywords: ['clean', 'tidy', 'organize', 'house', 'chore'],
        subtasks: [
            '- [ ] Gather cleaning supplies',
            '- [ ] Declutter surface areas',
            '- [ ] Dust and wipe down',
            '- [ ] Vacuum/Sweep floors',
            '- [ ] Take out trash'
        ]
    }
];

const GENERIC_SUBTASKS = [
    '- [ ] Define success criteria',
    '- [ ] Break down into smaller steps',
    '- [ ] Execute first step',
    '- [ ] Review progress',
    '- [ ] Mark as complete'
];

export async function simulateAIDecomposition(taskTitle: string): Promise<string> {
    // Simulate network latency for "AI" feel
    await new Promise(resolve => setTimeout(resolve, 1500));

    const lowerTitle = taskTitle.toLowerCase();

    const match = TEMPLATES.find(t =>
        t.keywords.some(k => lowerTitle.includes(k))
    );

    const steps = match ? match.subtasks : GENERIC_SUBTASKS;

    return `\n\n### 🪄 AI Suggested Steps:\n${steps.join('\n')}`;
}
