<?php

namespace App\Http\Controllers\Api\Admin;

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

class AdminServiceController extends BaseController
{
    public function index(Request $request): JsonResponse
    {
        $services = BillService::with('variations')->orderBy('sort_order')->get();
        return $this->successResponse($services);
    }

    public function store(Request $request): JsonResponse
    {
        $validated = $request->validate([
            'name' => ['required', 'string', 'max:255'],
            'slug' => ['required', 'string', 'max:255', 'unique:bill_services'],
            'code' => ['required', 'string', 'max:50', 'unique:bill_services'],
            'category' => ['required', 'in:airtime,data,cable,electricity,exam_pin,other'],
            'icon' => ['nullable', 'string'],
            'description' => ['nullable', 'string'],
            'min_amount' => ['nullable', 'numeric'],
            'max_amount' => ['nullable', 'numeric'],
            'service_charge' => ['nullable', 'numeric'],
            'cashback_percent' => ['nullable', 'numeric'],
            'sort_order' => ['nullable', 'integer'],
        ]);

        $service = BillService::create($validated);

        return $this->successResponse($service, 'Service created', 201);
    }

    public function update(Request $request, BillService $service): JsonResponse
    {
        $validated = $request->validate([
            'name' => ['sometimes', 'string', 'max:255'],
            'is_active' => ['sometimes', 'boolean'],
            'min_amount' => ['sometimes', 'numeric'],
            'max_amount' => ['sometimes', 'numeric'],
            'service_charge' => ['sometimes', 'numeric'],
            'cashback_percent' => ['sometimes', 'numeric'],
            'sort_order' => ['sometimes', 'integer'],
            'description' => ['nullable', 'string'],
        ]);

        $service->update($validated);

        return $this->successResponse($service->fresh(), 'Service updated');
    }

    public function storeVariation(Request $request, BillService $service): JsonResponse
    {
        $validated = $request->validate([
            'name' => ['required', 'string', 'max:255'],
            'code' => ['required', 'string', 'max:50'],
            'amount' => ['nullable', 'numeric'],
            'discount' => ['nullable', 'numeric'],
            'sort_order' => ['nullable', 'integer'],
        ]);

        $variation = BillVariation::create([
            'bill_service_id' => $service->id,
            ...$validated,
        ]);

        return $this->successResponse($variation, 'Variation created', 201);
    }

    public function updateVariation(Request $request, BillVariation $variation): JsonResponse
    {
        $validated = $request->validate([
            'name' => ['sometimes', 'string', 'max:255'],
            'amount' => ['sometimes', 'numeric'],
            'discount' => ['sometimes', 'numeric'],
            'is_active' => ['sometimes', 'boolean'],
            'sort_order' => ['sometimes', 'integer'],
        ]);

        $variation->update($validated);

        return $this->successResponse($variation->fresh(), 'Variation updated');
    }
}
