import type { PrismaClient } from '@prisma/client';
import type { OpenAI } from 'openai';
import type Stripe from 'stripe';
import type { OAuth2Client } from 'google-auth-library';
import type { FastifyRequest, FastifyReply } from 'fastify';
import type { MultipartFile } from '@fastify/multipart';
import '@fastify/jwt';

declare module '@fastify/jwt' {
  interface FastifyJWT {
    payload: { id: string };
    user: { id: string };
  }
}

export interface RecipeData {
  titre: string;
  ingredients: string | string[];
  etapes: string | string[];
  temps_preparation?: number;
  temps_cuisson?: number;
  portions?: number;
  difficulte?: string;
  conseils?: string;
  image_url?: string | null;
  /** Forme structurée originale renvoyée par l'IA, pour stockage en JSON */
  structured?: unknown;
}

export interface AudioSaveResult {
  success: boolean;
  url?: string;
  path?: string;
  localPath?: string;
  isLocal?: boolean;
}

export type AudioInput = string | AudioSaveResult;

declare module 'fastify' {
  interface FastifyInstance {
    prisma: PrismaClient;

    // auth plugin
    authenticate: (request: FastifyRequest, reply: FastifyReply) => Promise<void>;
    hashPassword: (password: string) => Promise<string>;
    comparePassword: (password: string, hash: string) => Promise<boolean>;

    // stripe plugin (optionnel: désactivé si STRIPE_SECRET_KEY absent)
    stripe?: Stripe;
    createCustomer?: (email: string, name: string) => Promise<Stripe.Customer>;
    createSubscription?: (
      customerId: string,
      priceId: string
    ) => Promise<Stripe.Subscription>;

    // ai plugin
    openai: OpenAI;
    transcribeAudio: (audioInput: AudioInput) => Promise<string>;
    generateRecipe: (
      ingredients: string,
      prompt?: string,
      preferences?: unknown
    ) => Promise<RecipeData>;
    saveAudioFile: (file: MultipartFile) => Promise<AudioSaveResult>;

    // google-auth plugin
    googleClient: OAuth2Client;
  }

}
