'use server';

/**
 * @fileOverview Personalized trail recommendations flow.
 *
 * This file defines a Genkit flow that provides personalized trail recommendations based on user riding history, skill level, and bike type.
 *
 * @exports {personalizedTrailRecommendations} - The main function to get trail recommendations.
 * @exports {PersonalizedTrailRecommendationsInput} - The input type for the personalizedTrailRecommendations function.
 * @exports {PersonalizedTrailRecommendationsOutput} - The output type for the personalizedTrailRecommendations function.
 */

import {ai} from '@/ai/genkit';
import {z} from 'genkit';


const PersonalizedTrailRecommendationsInputSchema = z.object({
  ridingHistory: z.string().describe('A summary of the user\u2019s past riding experiences, including trails ridden, dates, and any notable events.'),
  skillLevel: z.enum(['beginner', 'intermediate', 'advanced']).describe('The user\u2019s self-assessed skill level.'),
  bikeType: z.string().describe('The type of bike the user rides (e.g., enduro, trail, downhill).'),
  desiredTrailCharacteristics: z.string().describe('A description of the kind of trail the user is looking for.')
});
export type PersonalizedTrailRecommendationsInput = z.infer<typeof PersonalizedTrailRecommendationsInputSchema>;

const TrailRecommendationSchema = z.object({
  trailName: z.string().describe('The name of the recommended trail.'),
  location: z.string().describe('The location of the trail.'),
  difficulty: z.enum(['beginner', 'intermediate', 'advanced']).describe('The difficulty level of the trail.'),
  description: z.string().describe('A brief description of the trail, highlighting key features and points of interest.'),
  reasoning: z.string().describe('The reason why this trail is recommended to the user, based on their input data.'),
  link: z.string().describe('A link to more information about the trail.')
});

const PersonalizedTrailRecommendationsOutputSchema = z.object({
  recommendations: z.array(TrailRecommendationSchema).describe('An array of personalized trail recommendations.')
});
export type PersonalizedTrailRecommendationsOutput = z.infer<typeof PersonalizedTrailRecommendationsOutputSchema>;

export async function personalizedTrailRecommendations(input: PersonalizedTrailRecommendationsInput): Promise<PersonalizedTrailRecommendationsOutput> {
  return personalizedTrailRecommendationsFlow(input);
}

const personalizedTrailRecommendationsPrompt = ai.definePrompt({
  name: 'personalizedTrailRecommendationsPrompt',
  input: {schema: PersonalizedTrailRecommendationsInputSchema},
  output: {schema: PersonalizedTrailRecommendationsOutputSchema},
  prompt: `You are an expert trail recommendation engine. Given the following information about a user, provide personalized trail recommendations that match their riding history, skill level, bike type and preferences.

User Riding History: {{{ridingHistory}}}
User Skill Level: {{{skillLevel}}}
Bike Type: {{{bikeType}}}
Desired Trail Characteristics: {{{desiredTrailCharacteristics}}}

Consider all these factors to provide relevant trail suggestions. For each trail, explain why it is a good fit for the user.`
});

const personalizedTrailRecommendationsFlow = ai.defineFlow(
  {
    name: 'personalizedTrailRecommendationsFlow',
    inputSchema: PersonalizedTrailRecommendationsInputSchema,
    outputSchema: PersonalizedTrailRecommendationsOutputSchema,
  },
  async input => {
    const {output} = await personalizedTrailRecommendationsPrompt(input);
    return output!;
  }
);
