<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\PartnerShow;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class PartnerShowController extends Controller
{
    /**
     * Display a listing of the partner's shows.
     */
    public function index(Request $request): JsonResponse
    {
        $user = $request->user();

        if (! $user || ! $user->isPartner()) {
            return response()->json([
                'error' => 'Unauthorized',
                'message' => 'Only partners can access this endpoint',
            ], 403);
        }

        $query = PartnerShow::where('partner_id', $user->id);

        // Filter by status
        if ($request->has('status')) {
            $status = $request->get('status');
            if ($status === 'active') {
                $query->where('is_active', true);
            } elseif ($status === 'inactive') {
                $query->where('is_active', false);
            } elseif ($status === 'live') {
                $query->where('is_live', true);
            }
        }

        // Filter by type
        if ($request->has('type')) {
            $query->where('type', $request->get('type'));
        }

        // Search
        if ($request->has('search')) {
            $search = $request->get('search');
            $query->where(function ($q) use ($search) {
                $q->where('title', 'like', "%{$search}%")
                    ->orWhere('showId', 'like', "%{$search}%");
            });
        }

        // Pagination
        $perPage = min($request->get('per_page', 15), 100);
        $shows = $query->latest()->paginate($perPage);

        return response()->json([
            'data' => $shows->items(),
            'meta' => [
                'current_page' => $shows->currentPage(),
                'last_page' => $shows->lastPage(),
                'per_page' => $shows->perPage(),
                'total' => $shows->total(),
            ],
        ]);
    }

    /**
     * Display the specified show.
     */
    public function show(Request $request, PartnerShow $show): JsonResponse
    {
        $user = $request->user();

        if (! $user || ! $user->isPartner()) {
            return response()->json([
                'error' => 'Unauthorized',
                'message' => 'Only partners can access this endpoint',
            ], 403);
        }

        // Check if show belongs to partner
        if ($show->partner_id !== $user->id) {
            return response()->json([
                'error' => 'Not Found',
                'message' => 'Show not found',
            ], 404);
        }

        return response()->json([
            'data' => $show,
        ]);
    }

    /**
     * Update the specified show.
     */
    public function update(Request $request, PartnerShow $show): JsonResponse
    {
        $user = $request->user();

        if (! $user || ! $user->isPartner()) {
            return response()->json([
                'error' => 'Unauthorized',
                'message' => 'Only partners can access this endpoint',
            ], 403);
        }

        // Check if show belongs to partner
        if ($show->partner_id !== $user->id) {
            return response()->json([
                'error' => 'Not Found',
                'message' => 'Show not found',
            ], 404);
        }

        $validated = $request->validate([
            'title' => ['sometimes', 'string', 'max:255'],
            'description' => ['nullable', 'string'],
            'start_time' => ['sometimes', 'integer'],
            'type' => ['sometimes', 'string', 'max:50'],
            'price' => ['sometimes', 'numeric', 'min:0'],
            'banner_link' => ['nullable', 'string', 'max:255', 'url'],
            'is_promo' => ['sometimes', 'boolean'],
            'promo_price' => ['nullable', 'numeric', 'min:0'],
            'is_active' => ['sometimes', 'boolean'],
            'is_live' => ['sometimes', 'boolean'],
            'is_replay' => ['sometimes', 'boolean'],
            'slug_live' => ['sometimes', 'string', 'max:200'],
            'url_live' => ['nullable', 'string', 'max:255', 'url'],
            'url_replay' => ['nullable', 'string', 'max:255', 'url'],
        ]);

        $show->update($validated);

        return response()->json([
            'message' => 'Show updated successfully',
            'data' => $show->fresh(),
        ]);
    }
}

