<?php

namespace TPS\API\Http\Controllers;

use Illuminate\Http\Request;
use TPS\API\Http\Requests\AddressRequest;
use Webkul\Customer\Repositories\CustomerAddressRepository;
use Webkul\RestApi\Http\Controllers\V1\Shop\Customer\AddressController;
use TPS\API\Http\Resources\Customer\Address;

class Addresses extends AddressController
{
    /**
     * Get customer addresses.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $customer = $request->user();

        $addresses = $customer->addresses()->get();

        return response([
            'data' => Address::collection($addresses),
        ]);
    }
    /**
     * Store address.
     *
     * @param  \Webkul\Customer\Http\Requests\CustomerAddressRequest  $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function create(AddressRequest $request)
    {
        $data = $request->all();
        $data['address1'] = implode(PHP_EOL, array_filter($data['address1']));
        $data['customer_id'] = $request->user()->id;
        $data['country'] = 'Turkmenistan';
        $data['postcode'] = '0000';

        $customerAddress = $this->getRepositoryInstance()->create($data);

        return response([
            'data'    => new Address($customerAddress),
            'message' => 'Your address has been created successfully.',
        ]);
    }
}