<?php

namespace App\Http\Controllers\API;

use App\Http\Controllers\Controller;
use App\Http\Requests\Api\Service\ServiceRequest;
use App\Http\Resources\ProblemResource;
use App\Http\Resources\ServiceRequestResource;
use App\Http\Resources\ServiceResource;
use App\Models\Problem;
use App\Models\Service;
use App\Models\ServiceRequest as ModelsServiceRequest;

class ServiceController extends Controller
{
    public function index()
    {
        $services = Service::orderBy('position', 'asc')->get();
        return ServiceResource::collection($services);
    }

    public function store(ServiceRequest $request)
    {
        $user = $request->user();

        $serviceRequest = ModelsServiceRequest::create([
            'user_id' => $user->id,
            'service_id' => $request->service_id,
            'power_id' => $request->power_id,
            'city_id' => $request->city_id,
            'phone' => $request->phone,
            'comment' => $request->comment,
            'full_name' => $request->full_name,
            'status' => 'pending',
            'problem_id' => $request->problem_id
        ]);

        return new ServiceRequestResource($serviceRequest);
    }

    public function show($id)
    {
        $service = Service::findOrFail($id);
        return new ServiceResource($service);
    }

    public function getProblems()
    {
        $problems = Problem::all();
        return ProblemResource::collection($problems);
    }
}
