<?php

namespace App\Http\Controllers\Admin;

use App\Enums\DbStatusFieldEnum;
use App\Http\Controllers\Controller;
use App\Models\Address;
use App\Services\AddressService;
use App\Traits\JsonApiCustomFunctionTraits;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Validation\ValidationException;
use Validator;

class AddressController extends Controller
{
    use JsonApiCustomFunctionTraits;

    protected AddressService $service;

    public function __construct(AddressService $service)
    {
        $this->service = $service;
    }

    /**
     * Display a listing of the resource.
     */
    public function index(): Response|JsonResponse
    {
        return response()->noContent();
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create(): Response|JsonResponse
    {
        return response('create');
    }

    /**
     * Store a newly created resource in storage.
     *
     *
     * @throws ValidationException
     */
    public function store(Request $request): Response|JsonResponse
    {
        $validated = Validator::make($request->all(), [
            'street' => 'required',
            'name' => 'string|nullable|max:250',
            'postCode' => 'string|nullable|max:250',
            'city' => 'string|nullable|max:250',
            'note' => 'string|nullable|max:65000',
        ]);

        if ($validated->fails()) {
            return response()->json($this->convertMessageBagToJSONAPIError($validated->getMessageBag()));
        }

        $newItem = new Address;
        $newItem->fill($validated->validated());
        $newItem->status = DbStatusFieldEnum::active;
        $newItem->save();

        return response()->json($newItem->toArray());
    }

    /**
     * Display the specified resource.
     */
    public function show(int $ContactId): Response|JsonResponse
    {

        return response()->json(Address::findOrFail($ContactId));
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(int $ContactId): Response|JsonResponse
    {
        return response()->noContent();
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, int $ItemId): Response|JsonResponse
    {
        $validated = Validator::make($request->all(), [
            'street' => 'required',
            'name' => 'string|nullable|max:250',
            'postCode' => 'string|nullable|max:250',
            'city' => 'string|nullable|max:250',
            'note' => 'string|nullable|max:65000',
        ]);

        if ($validated->fails()) {
            return response()->json($this->convertMessageBagToJSONAPIError($validated->getMessageBag()));
        }

        $newItem = Address::findOrFail($ItemId);
        $newItem->fill($validated->validated());

        $newItem->save();

        return response()->json($newItem->toArray());

    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(int $ContactId): Response|JsonResponse
    {
        $item = Address::findOrFail($ContactId);
        $item->delete();

        return response()->json(['success' => true]);
    }
}
