<?php

namespace App\Services\AI;

use App\Models\User;
use App\Repositories\ExerciseRepository;

class RecommendationEngine
{
    protected $exerciseRepo;

    public function __construct(ExerciseRepository $exerciseRepo)
    {
        $this->exerciseRepo = $exerciseRepo;
    }

    public function generate(User $user, string $message, string $response): array
    {
        $recommendations = [];
        $keywords = ['stress', 'anxiety', 'sleep', 'focus', 'gratitude'];

        foreach ($keywords as $keyword) {
            if (stripos($message, $keyword) !== false || stripos($response, $keyword) !== false) {
                $exercises = $this->exerciseRepo->findByField('category', $keyword);
                foreach ($exercises as $ex) {
                    $recommendations[] = [
                        'title' => $ex->title,
                        'slug' => $ex->slug,
                        'reason' => "Based on your mention of '{$keyword}'",
                    ];
                }
            }
        }

        return array_slice($recommendations, 0, 3);
    }
}
