<?php

namespace App\Http\Controllers;

use App\Models\Favorite;
use App\Models\Product;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class FavoriteController extends Controller
{
    /**
     * Get all favorites for the authenticated user
     * GET /api/favorites
     */
    public function index(Request $request)
    {
        $user = Auth::user();
        $perPage = $request->query('per_page', 15);

        $favorites = Favorite::where('user_id', $user->id)
            ->with('product')
            ->orderBy('created_at', 'desc')
            ->paginate($perPage);

        return response()->json([
            'success' => true,
            'message' => 'Favorites retrieved successfully',
            'data' => $favorites->items(),
            'pagination' => [
                'total' => $favorites->total(),
                'count' => $favorites->count(),
                'per_page' => $favorites->perPage(),
                'current_page' => $favorites->currentPage(),
                'total_pages' => $favorites->lastPage(),
            ],
        ], 200);
    }

    /**
     * Add a product to user's favorites
     * POST /api/favorites
     */
    public function store(Request $request)
    {
        $request->validate([
            'product_id' => 'required|exists:products,id',
        ]);

        $user = Auth::user();
        $productId = $request->product_id;

        // Check if already favorited
        $existingFavorite = Favorite::where('user_id', $user->id)
            ->where('product_id', $productId)
            ->first();

        if ($existingFavorite) {
            return response()->json([
                'success' => false,
                'message' => 'Product is already in your favorites',
                'data' => $existingFavorite,
            ], 409);
        }

        $favorite = Favorite::create([
            'user_id' => $user->id,
            'product_id' => $productId,
        ]);

        $favorite->load('product');

        return response()->json([
            'success' => true,
            'message' => 'Product added to favorites successfully',
            'data' => $favorite,
        ], 201);
    }

    /**
     * Remove a product from user's favorites
     * DELETE /api/favorites/{product_id}
     */
    public function destroy($productId)
    {
        $user = Auth::user();

        $favorite = Favorite::where('user_id', $user->id)
            ->where('product_id', $productId)
            ->first();

        if (!$favorite) {
            return response()->json([
                'success' => false,
                'message' => 'Product not found in your favorites',
            ], 404);
        }

        $favorite->delete();

        return response()->json([
            'success' => true,
            'message' => 'Product removed from favorites successfully',
        ], 200);
    }

    /**
     * Check if a product is in user's favorites
     * GET /api/favorites/check/{product_id}
     */
    public function check($productId)
    {
        $user = Auth::user();

        $isFavorited = Favorite::where('user_id', $user->id)
            ->where('product_id', $productId)
            ->exists();

        return response()->json([
            'success' => true,
            'message' => 'Favorite status retrieved',
            'data' => [
                'product_id' => $productId,
                'is_favorited' => $isFavorited,
            ],
        ], 200);
    }
}
