<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\EventPackageAddon;
use App\Services\Paddle\PaddleTransactionService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Log;

class TenantBillingController extends Controller
{
    public function __construct(private readonly PaddleTransactionService $paddleTransactions) {}

    public function transactions(Request $request): JsonResponse
    {
        $tenant = $request->attributes->get('tenant');

        if (! $tenant) {
            return response()->json([
                'data' => [],
                'message' => 'Tenant not found.',
            ], 404);
        }

        if (! $tenant->paddle_customer_id) {
            return response()->json([
                'data' => [],
                'message' => 'Tenant has no Paddle customer identifier.',
            ]);
        }

        $cursor = $request->query('cursor');
        $perPage = (int) $request->query('per_page', 25);

        $query = [
            'per_page' => max(1, min($perPage, 100)),
        ];

        if ($cursor) {
            $query['after'] = $cursor;
        }

        try {
            $result = $this->paddleTransactions->listForCustomer($tenant->paddle_customer_id, $query);
        } catch (\Throwable $exception) {
            Log::warning('Failed to load Paddle transactions', [
                'tenant_id' => $tenant->id,
                'error' => $exception->getMessage(),
            ]);

            return response()->json([
                'data' => [],
                'message' => 'Failed to load Paddle transactions.',
            ], 502);
        }

        return response()->json([
            'data' => $result['data'],
            'meta' => $result['meta'],
        ]);
    }

    public function addons(Request $request): JsonResponse
    {
        $tenant = $request->attributes->get('tenant');

        if (! $tenant) {
            return response()->json([
                'data' => [],
                'message' => 'Tenant not found.',
            ], 404);
        }

        $perPage = max(1, min((int) $request->query('per_page', 25), 100));
        $page = max(1, (int) $request->query('page', 1));

        $paginator = EventPackageAddon::query()
            ->where('tenant_id', $tenant->id)
            ->with(['event:id,name,slug'])
            ->orderByDesc('purchased_at')
            ->orderByDesc('created_at')
            ->paginate($perPage, ['*'], 'page', $page);

        $data = $paginator->getCollection()->map(function (EventPackageAddon $addon) {
            return [
                'id' => $addon->id,
                'addon_key' => $addon->addon_key,
                'label' => $addon->metadata['label'] ?? null,
                'quantity' => (int) ($addon->quantity ?? 1),
                'status' => $addon->status,
                'amount' => $addon->amount !== null ? (float) $addon->amount : null,
                'currency' => $addon->currency,
                'extra_photos' => (int) $addon->extra_photos,
                'extra_guests' => (int) $addon->extra_guests,
                'extra_gallery_days' => (int) $addon->extra_gallery_days,
                'purchased_at' => $addon->purchased_at?->toIso8601String(),
                'receipt_url' => Arr::get($addon->receipt_payload, 'receipt_url'),
                'event' => $addon->event ? [
                    'id' => $addon->event->id,
                    'slug' => $addon->event->slug,
                    'name' => $addon->event->name,
                ] : null,
            ];
        })->values();

        return response()->json([
            'data' => $data,
            'meta' => [
                'current_page' => $paginator->currentPage(),
                'last_page' => $paginator->lastPage(),
                'per_page' => $paginator->perPage(),
                'total' => $paginator->total(),
            ],
        ]);
    }
}
