<?php

namespace App\Http\Controllers;

use App\Models\Address;
use App\Models\Customer;
use Illuminate\Support\Facades\Log;
use Illuminate\Http\Request;
use GuzzleHttp\Client;
use Illuminate\Validation\Rule;

class AddressController extends Controller
{
    public function getPincodeDetails($pincode) {
        // log::info($pincode);
        $client = new Client();
        // $api = $client->request('GET', 'https://api.postalpincode.in/pincode/'.$pincode);
        $api = $client->request('GET', 'https://api.postalpincode.in/pincode/'.$pincode);

        $response = json_decode($api->getBody())[0]->PostOffice;
        // log::info($response);

        $areas = [];
        $i = 1;
        foreach ($response as $value) {
            array_push($areas, ['id' => $i++, 'name' => get_object_vars($value)['Name']]);
        }
        $data = [];
        $data['District'] = get_object_vars($response[0])['District'];
        $data['State'] = get_object_vars($response[0])['State'];
        $data['Division'] = get_object_vars($response[0])['Division'];
        $data['Country'] = get_object_vars($response[0])['Country'];
        $data['areas'] = $areas;
        // log::info($data);
        
        if ($data){
            return response()->json(['success' => true,'message' => 'Pincode Details get Successfully', 'data' => $data],200);
        }else{
            return response()->json(['status' => false,'data' => 'Failed Get Pincode Details'],200);
        }
    
    }
    

    ##STORE
    public function storeaddress(Request $request){
        log::info($request);
        $validate   =   $request->validate([
            'customer_id'                    => 'required',
            'email'                          => 'required',
            'country'                        => 'required',
            'first_name'                     => 'required',
            'last_name'                      => 'required',
            'adresses'                       => 'required',
            'apartment'                      => 'required',
            'city'                           => 'required',
            'state'                          => 'required',
            'pincode'                        => 'required',
            'phone_number'                   => 'required',
        ]);
     
        $storedata                                  = new Address();
        $storedata->customer_id                     = $request->customer_id;
        $storedata->email                           = $request->email;
        $storedata->country                         = $request->country;
        $storedata->first_name                      = $request->first_name;
        $storedata->last_name                       = $request->last_name;
        $storedata->adresses                        = $request->adresses;
        $storedata->apartment                       = $request->apartment;
        $storedata->city                            = $request->city;
        $storedata->state                           = $request->state;
        $storedata->pincode                         = $request->pincode;
        $storedata->phone_number                    = $request->phone_number;
        $store                                      =  $storedata->save();

        if ($store) {   
            return response()->json(['status' => true, 'data' => $storedata], 200);
        } 
        else{
            return response()->json(['status' => false,'data' => 'failed Store'],200);
        }
    }

}
