<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Resources\CustomerResource;
use App\Services\CustomerService;
use App\Traits\ApiResponse;
use Illuminate\Http\Request;

class CustomerController extends Controller
{
    use ApiResponse;

    protected $customerService;

    public function __construct(CustomerService $customerService)
    {
        $this->customerService = $customerService;
    }

    public function login(Request $request)
    {
        $request->validate([
            'identifier' => 'required|string',
            'password' => 'required|string',
            'business_code' => 'nullable|string',
        ]);

        try {
            $result = $this->customerService->login($request->all(), $request->ip());

            if (isset($result['error']) && $result['error'] === 'lockout') {
                return $this->errorResponse(
                    $result['error'], 
                    429, 
                    [
                        'seconds_remaining' => $result['seconds'],
                        'locked_until'      => $result['until']
                    ]
                );
            }

            if (isset($result['error'])) {
                return $this->errorResponse($result['error'], 401);
            }

            return $this->successResponse([
                'access_token' => $result['token'],
                'customer' => new CustomerResource($result['customer'])
            ], 'login success', 200);
        } catch(\Exception $e) {
            return $this->errorResponse('internal server error', 500, $e->getMessage());
        }
    }

    public function register(Request $request)
    {
        try {
            $this->customerService->register($request->all());
            return $this->successResponse(null, 'registration success', 201);
        } catch (\Exception $e) {
            return $this->errorResponse('internal server error', 500, $e->getMessage());
        }
    }

    public function updatePassword(Request $request, $customerId)
    {
        try {
            $result = $this->customerService->updatePassword($request->all(), $customerId);

            if(isset($result['error'])) {
                return $this->errorResponse('internal server error', 500, $result['error']);
            }
            return $this->successResponse(null, 'update password success', 201);
        } catch (\Exception $e) {
            return $this->errorResponse('internal server error', 500, $e->getMessage());
        }
    }

    public function updateCustomer(Request $request, $customerId)
    {
        try {
            $data = $request->all();
            $customerInfo = $this->customerService->updateCustomer($data, $customerId);
            return $this->successResponse($customerInfo, 'Update customer successfully');
        } catch(\Exception $e) {
            return $this->errorResponse('Failed to update customer', errorDetails: $e->getMessage());
        }
    }

    public function getCustomerAddress($customerId)
    {
        try {
            $customerAddress = $this->customerService->getCustomerAddress($customerId);
            return $this->successResponse($customerAddress);
        } catch (\Exception $e) {
            return $this->errorResponse('Failed to retrieve customer address', errorDetails: $e->getMessage());
        }
    }

    public function saveCustomerAddress(Request $request, $customerId)
    {
        try {
            $data = $request->all();
            $this->customerService->saveCustomerAddress($data, $customerId);
            return $this->successResponse(null, 'Save customer address successfully');
        } catch(\Exception $e) {
            return $this->errorResponse('Failed to save customer address', errorDetails: $e->getMessage());
        }
    }

    public function deleteCustomerAddress($customerId, $id)
    {
         try {
            $this->customerService->deleteCustomerAddress($customerId, $id);
            return $this->successResponse(null, 'Delete customer address successfully');
        } catch(\Exception $e) {
            return $this->errorResponse('Failed to delete customer address', errorDetails: $e->getMessage());
        }
    }
}
