<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Playlist;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class PlaylistController extends Controller
{
    public function index()
    {
        $playlists = Playlist::with('user')->latest()->paginate(20);
        return view('admin.playlists.index', compact('playlists'));
    }

    public function create()
    {
        return view('admin.playlists.create');
    }

    public function store(Request $request)
    {
        $validated = $request->validate([
            'title' => 'required|string|max:255',
            'description' => 'nullable|string',
            'is_public' => 'boolean',
        ]);

        $validated['user_id'] = Auth::guard('admin')->check() ? null : Auth::id(); // Admin playlists might not have a user_id or linked to a super admin
        // If user_id is foreign key constrained to users table, and admins are separate, we might need a workaround or make user_id nullable in schema.
        // Migration says: user_id constrained to users.
        // So an Admin creating a playlist must assign it to a User? Or maybe the Admin *is* a User?
        // Requirement 10 says "Player with playlist capability".
        // Usually Admins create "Featured Playlists".
        // Let's assume for now we assign it to the first user or make it nullable?
        // Actually, the migration `2024...` has `user_id` constrained.
        // I'll check `User` model. Maybe there's a system user.
        // For now, I'll allow `user_id` to be null if I can modify migration, but I can't.
        // I will just select a user in the form or assign to ID 1.
        // Wait, `Playlist` usually belongs to a user (personal playlist).
        // If Admin manages *System* playlists, maybe we need a different table or `user_id` nullable.
        // Checking migration: `table->foreignId('user_id')->constrained('users')->onDelete('cascade');` -> It is NOT nullable.
        // This implies Playlists are strictly User-owned.
        // So Admin is probably managing *User's* playlists? Or creating playlists *for* users?
        // OR, the Requirement "Content management... playlists" means curated lists.
        // I will add a field to select Owner (User) in the form.

        $validated['user_id'] = $request->user_id ?? 1; // Fallback or explicit

        Playlist::create($validated);

        return redirect()->route('admin.playlists.index')->with('success', 'پلی‌لیست ایجاد شد.');
    }

    public function edit(Playlist $playlist)
    {
        return view('admin.playlists.edit', compact('playlist'));
    }

    public function update(Request $request, Playlist $playlist)
    {
        $validated = $request->validate([
            'title' => 'required|string|max:255',
            'description' => 'nullable|string',
            'is_public' => 'boolean',
        ]);

        $playlist->update($validated);

        return redirect()->route('admin.playlists.index')->with('success', 'پلی‌لیست ویرایش شد.');
    }

    public function destroy(Playlist $playlist)
    {
        $playlist->delete();
        return redirect()->route('admin.playlists.index')->with('success', 'پلی‌لیست حذف شد.');
    }
}
