<?php

namespace App\Http\Controllers\Web\Admin;

use App\Http\Controllers\Controller;
use App\Models\BillService;
use App\Models\BillVariation;
use App\Modules\ProviderIntegration\Interfaces\ProviderInterface;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Artisan;

class AdminServiceController extends Controller
{
    public function index()
    {
        $services = BillService::with('variations')->orderBy('sort_order')->get();
        return view('admin.services.index', compact('services'));
    }

    public function store(Request $request)
    {
        $validated = $request->validate([
            'name' => ['required', 'string', 'max:255'],
            'slug' => ['required', 'string', 'unique:bill_services'],
            'code' => ['required', 'string', 'unique:bill_services'],
            'category' => ['required', 'in:airtime,data,cable,electricity,exam_pin,other'],
            'is_active' => ['sometimes', 'boolean'],
            'service_charge' => ['nullable', 'numeric'],
        ]);

        BillService::create($validated);

        return back()->with('success', 'Service created');
    }

    public function update(Request $request, BillService $service)
    {
        $validated = $request->validate([
            'name' => ['sometimes', 'string'],
            'is_active' => ['sometimes', 'boolean'],
            'service_charge' => ['sometimes', 'numeric'],
            'cashback_percent' => ['sometimes', 'numeric'],
        ]);

        $service->update($validated);

        return back()->with('success', 'Service updated');
    }

    public function sync(Request $request)
    {
        $output = Artisan::call('vtpass:sync-services');

        $results = Artisan::output();

        return back()->with('success', "Services synced from VTPass!\n{$results}");
    }
}
