<?php

namespace App\Http\Controllers;

use App\Models\Track;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class LikeController extends Controller
{
    public function like(Request $request, Track $track): JsonResponse
    {
        $request->user()->likes()->syncWithoutDetaching([$track->id]);

        return response()->json(['message' => 'Track liked.']);
    }

    public function unlike(Request $request, Track $track): JsonResponse
    {
        $request->user()->likes()->detach($track->id);

        return response()->json(['message' => 'Track unliked.']);
    }

    public function index(Request $request): JsonResponse
    {
        $tracks = $request->user()->likes()->with(['album', 'artists', 'genres'])->get();

        return response()->json($tracks);
    }
}
