<?php

declare(strict_types=1);

namespace App\Application\Shopify;

use App\Framework\Api\ApiException;
use App\Framework\Attributes\Route;
use App\Framework\Http\Method;
use App\Framework\Http\Status;
use App\Framework\Router\Result\JsonResult;
use App\Infrastructure\Api\ShopifyClient;
use Archive\Config\ApiConfig;

final class CustomerController
{
    private ShopifyClient $client;

    public function __construct()
    {
        $this->client = new ShopifyClient(
            ApiConfig::SHOPIFY_SHOP_DOMAIN->value,
            ApiConfig::SHOPIFY_ACCESS_TOKEN->value,
            ApiConfig::SHOPIFY_API_VERSION->value
        );
    }

    /**
     * Ruft alle Kunden ab
     */
    #[Route(path: '/api/shopify/customers', method: Method::GET)]
    public function listCustomers(): JsonResult
    {
        try {
            $options = [
                'limit' => 50,
                'fields' => 'id,email,first_name,last_name,orders_count,total_spent',
            ];

            $customers = $this->client->getCustomers($options);

            return new JsonResult([
                'success' => true,
                'data' => $customers,
            ]);
        } catch (ApiException $e) {
            $result = new JsonResult([
                'success' => false,
                'error' => $e->getMessage(),
            ]);
            $result->status = Status::from($e->getCode() ?: 500);

            return $result;
        }
    }

    /**
     * Ruft einen einzelnen Kunden ab
     */
    #[Route(path: '/api/shopify/customers/{id}', method: Method::GET)]
    public function getCustomer(int $id): JsonResult
    {
        try {
            $customer = $this->client->getCustomer($id);

            return new JsonResult([
                'success' => true,
                'data' => $customer,
            ]);
        } catch (ApiException $e) {
            $result = new JsonResult([
                'success' => false,
                'error' => $e->getMessage(),
            ]);
            $result->status = Status::from($e->getCode() ?: 500);

            return $result;
        }
    }

    /**
     * Erstellt einen neuen Kunden
     */
    #[Route(path: '/api/shopify/customers', method: Method::POST)]
    public function createCustomer(CustomerRequest $request): JsonResult
    {
        try {
            $customerData = [
                'first_name' => $request->firstName,
                'last_name' => $request->lastName,
                'email' => $request->email,
                'phone' => $request->phone ?? null,
                'verified_email' => $request->verifiedEmail ?? true,
                'addresses' => $request->addresses ?? [],
                'password' => $request->password ?? null,
                'password_confirmation' => $request->password ?? null,
                'send_email_welcome' => $request->sendWelcomeEmail ?? false,
            ];

            // Entferne leere Felder
            $customerData = array_filter($customerData, fn ($value) => $value !== null);

            $customer = $this->client->createCustomer($customerData);

            $result = new JsonResult([
                'success' => true,
                'data' => $customer,
            ]);
            $result->status = Status::CREATED;

            return $result;
        } catch (ApiException $e) {
            $result = new JsonResult([
                'success' => false,
                'error' => $e->getMessage(),
            ]);
            $result->status = Status::from($e->getCode() ?: 500);

            return $result;
        }
    }
}
