<?php

namespace App\Http\Controllers\Api\Beneficiary;

use App\Http\Controllers\Api\BaseController;
use App\Models\Beneficiary;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class BeneficiaryController extends BaseController
{
    public function index(Request $request): JsonResponse
    {
        $query = Beneficiary::where('user_id', $request->user()->id)
            ->orderBy('is_favorite', 'desc')
            ->orderBy('usage_count', 'desc');

        if ($type = $request->get('type')) {
            $query->where('type', $type);
        }

        $beneficiaries = $query->get();

        return $this->successResponse($beneficiaries);
    }

    public function store(Request $request): JsonResponse
    {
        $validated = $request->validate([
            'name' => ['required', 'string', 'max:255'],
            'type' => ['required', 'in:phone,meter,smartcard,customer_id,exam'],
            'number' => ['required', 'string', 'max:100'],
            'network' => ['nullable', 'string', 'max:50'],
            'service_type' => ['nullable', 'string', 'max:50'],
            'variation_code' => ['nullable', 'string', 'max:50'],
            'is_favorite' => ['sometimes', 'boolean'],
        ]);

        $beneficiary = Beneficiary::create([
            'user_id' => $request->user()->id,
            ...$validated,
        ]);

        return $this->successResponse($beneficiary, 'Beneficiary created', 201);
    }

    public function show(Request $request, Beneficiary $beneficiary): JsonResponse
    {
        if ($beneficiary->user_id !== $request->user()->id) {
            return $this->errorResponse('Not found', 404);
        }

        return $this->successResponse($beneficiary);
    }

    public function update(Request $request, Beneficiary $beneficiary): JsonResponse
    {
        if ($beneficiary->user_id !== $request->user()->id) {
            return $this->errorResponse('Not found', 404);
        }

        $validated = $request->validate([
            'name' => ['sometimes', 'string', 'max:255'],
            'number' => ['sometimes', 'string', 'max:100'],
            'network' => ['nullable', 'string', 'max:50'],
            'service_type' => ['nullable', 'string', 'max:50'],
            'variation_code' => ['nullable', 'string', 'max:50'],
            'is_favorite' => ['sometimes', 'boolean'],
        ]);

        $beneficiary->update($validated);

        return $this->successResponse($beneficiary->fresh(), 'Beneficiary updated');
    }

    public function destroy(Request $request, Beneficiary $beneficiary): JsonResponse
    {
        if ($beneficiary->user_id !== $request->user()->id) {
            return $this->errorResponse('Not found', 404);
        }

        $beneficiary->delete();

        return $this->successResponse(null, 'Beneficiary deleted');
    }

    public function toggleFavorite(Request $request, Beneficiary $beneficiary): JsonResponse
    {
        if ($beneficiary->user_id !== $request->user()->id) {
            return $this->errorResponse('Not found', 404);
        }

        $beneficiary->update(['is_favorite' => !$beneficiary->is_favorite]);

        return $this->successResponse(
            $beneficiary->fresh(),
            $beneficiary->is_favorite ? 'Added to favorites' : 'Removed from favorites'
        );
    }
}
