<?php

namespace App\Http\Controllers;

use App\Services\OrganisationService;
use App\Traits\Api;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;
use Illuminate\Validation\Rules\File;


class OrganisationController extends Controller
{
    use Api;
    /**
     * Display a listing of the resource.
     */
    public function index(OrganisationService $organisationService)
    {
        return new JsonResponse($organisationService->all());
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request, OrganisationService $organisationService)
    {

        // dd($request->all());
        // $data = $request->validate(
        //     [
        //         "business_name" => "nullable|string",
        //         // 'business_logo' => 'sometimes|mimes:jpeg,png,jpg,webp|max:2048',
        //         "business_email" => "nullable|email",
        //         "business_size_id" => 'nullable|numeric',
        //         "business_industry_id" => 'nullable|numeric',
        //         "country_id" => 'nullable|numeric',
        //         "state_id" => 'nullable|numeric',
        //         "city" => "nullable|string",
        //         "street" => "nullable|string",
        //         "website_url" => "nullable|string",
        //         "phone_numbers" => 'nullable|array',
        //         "registration_no" => "nullable|string",

        //     ],
        //     [
        //         'business_logo' => [
        //             'sometimes',
        //             function ($attribute, $value, $fail) {
        //                 if (!is_string($value)) {
        //                     $fail('The :attribute must be a string.');
        //                 }

        //                 if (!preg_match('%^[a-zA-Z0-9/+]*={0,2}$%', $value)) {
        //                     $fail('The :attribute must be a valid base64 string.');
        //                 }

        //                 $decoded = base64_decode($value, true);
        //                 if ($decoded === false) {
        //                     $fail('The :attribute must be a valid base64 string.');
        //                 }

        //                 $finfo = finfo_open(FILEINFO_MIME_TYPE);
        //                 $mime = finfo_buffer($finfo, $decoded);
        //                 finfo_close($finfo);

        //                 if (strpos($mime, 'image/') !== 0) {
        //                     $fail('The :attribute must be a valid image in base64 format.');
        //                 }
        //             },
        //         ],
        //     ],
        //     [
        //         'business_size_id.numeric' => 'Invalid Businesss size selected',
        //         'country_id.numeric' => 'Invalid Country selected',
        //         'state_id.numeric' => 'Invalid State selected',
        //         'business_industry_id.numeric' => 'Invalid Business industry selected',
        //     ]
        // );

        // dd($request->all());

        $response = $organisationService->update($request->all());
        // dd($response);
        return $this->processApiResponseToBack($response);
    }


    public function storeLogo(Request $request): JsonResponse
    {
        $formData = $request->all();


        // dd($formData);

        $response = $this->patch(config('services.kedebah.hr_service_url') . "/organization-data", $formData);
        // dd($formData, $response->json(), $response->status());
        return new JsonResponse($response->json(), $response->status());
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id)
    {
        //
    }
}
