<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Requests\Checkout\CheckoutSessionStatusRequest;
use App\Models\CheckoutSession;
use App\Models\Package;
use App\Models\PackagePurchase;
use App\Models\TenantPackage;
use App\Services\Checkout\CheckoutSessionService;
use App\Services\Paddle\PaddleCheckoutService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\ValidationException;

class PackageController extends Controller
{
    public function __construct(
        private readonly PaddleCheckoutService $paddleCheckout,
        private readonly CheckoutSessionService $sessions,
    ) {}

    public function index(Request $request): JsonResponse
    {
        $type = $request->query('type', 'endcustomer');
        $packages = Package::where('type', $type)
            ->orderBy('price')
            ->get();

        $packages->each(function ($package) {
            if (is_string($package->features)) {
                $decoded = json_decode($package->features, true);
                $package->features = is_array($decoded) ? $decoded : [];

                return;
            }

            if (! is_array($package->features)) {
                $package->features = [];
            }
        });

        return response()->json([
            'data' => $packages,
            'message' => "Packages for type '{$type}' loaded successfully.",
        ]);
    }

    public function purchase(Request $request): JsonResponse
    {
        $request->validate([
            'package_id' => 'required|exists:packages,id',
            'type' => 'required|in:endcustomer,reseller',
            'payment_method' => 'required|in:paddle',
            'event_id' => 'nullable|exists:events,id', // For endcustomer
            'success_url' => 'nullable|url',
            'return_url' => 'nullable|url',
        ]);

        $package = Package::findOrFail($request->package_id);
        $tenant = $request->attributes->get('tenant');

        if (! $tenant) {
            throw ValidationException::withMessages(['tenant' => 'Tenant not found.']);
        }

        if ($package->price == 0) {
            // Free package - direct assignment
            return $this->handleFreePurchase($request, $package, $tenant);
        }

        // Paid purchase
        return $this->handlePaidPurchase($request, $package, $tenant);
    }

    public function completePurchase(Request $request): JsonResponse
    {
        $request->validate([
            'package_id' => 'required|exists:packages,id',
            'paddle_transaction_id' => 'required|string',
        ]);

        $package = Package::findOrFail($request->package_id);
        $tenant = $request->attributes->get('tenant');

        if (! $tenant) {
            throw ValidationException::withMessages(['tenant' => 'Tenant not found.']);
        }

        $provider = 'paddle';

        DB::transaction(function () use ($request, $package, $tenant, $provider) {
            PackagePurchase::create([
                'tenant_id' => $tenant->id,
                'package_id' => $package->id,
                'provider' => $provider,
                'provider_id' => $request->input('paddle_transaction_id'),
                'price' => $package->price,
                'type' => 'endcustomer_event',
                'purchased_at' => now(),
                'metadata' => json_encode(['note' => 'Wizard purchase', 'provider' => $provider]),
            ]);

            TenantPackage::create([
                'tenant_id' => $tenant->id,
                'package_id' => $package->id,
                'price' => $package->price,
                'purchased_at' => now(),
                'active' => true,
            ]);
        });

        return response()->json([
            'message' => 'Purchase completed successfully.',
            'provider' => $provider,
        ], 201);
    }

    public function assignFree(Request $request): JsonResponse
    {
        $request->validate([
            'package_id' => 'required|exists:packages,id',
        ]);

        $package = Package::findOrFail($request->package_id);
        $tenant = $request->attributes->get('tenant');

        if (! $tenant) {
            throw ValidationException::withMessages(['tenant' => 'Tenant not found.']);
        }

        if ($package->price != 0) {
            throw ValidationException::withMessages(['package' => 'Not a free package.']);
        }

        DB::transaction(function () use ($package, $tenant) {
            PackagePurchase::create([
                'tenant_id' => $tenant->id,
                'package_id' => $package->id,
                'provider' => 'free',
                'provider_id' => 'free_wizard',
                'price' => $package->price,
                'type' => 'endcustomer_event',
                'purchased_at' => now(),
                'metadata' => json_encode(['note' => 'Free via wizard']),
            ]);

            TenantPackage::create([
                'tenant_id' => $tenant->id,
                'package_id' => $package->id,
                'price' => $package->price,
                'purchased_at' => now(),
                'active' => true,
            ]);
        });

        return response()->json([
            'message' => 'Free package assigned successfully.',
        ], 201);
    }

    public function createPaddleCheckout(Request $request): JsonResponse
    {
        $request->validate([
            'package_id' => 'required|exists:packages,id',
            'success_url' => 'nullable|url',
            'return_url' => 'nullable|url',
        ]);

        $package = Package::findOrFail($request->integer('package_id'));
        $tenant = $request->attributes->get('tenant');
        $user = $request->user();

        if (! $tenant) {
            throw ValidationException::withMessages(['tenant' => 'Tenant context missing.']);
        }

        if (! $user) {
            throw ValidationException::withMessages(['user' => 'User context missing.']);
        }

        if (! $package->paddle_price_id) {
            throw ValidationException::withMessages(['package_id' => 'Package is not linked to a Paddle price.']);
        }

        $session = $this->sessions->createOrResume($user, $package, [
            'tenant' => $tenant,
        ]);

        $this->sessions->selectProvider($session, CheckoutSession::PROVIDER_PADDLE);

        $now = now();

        $session->forceFill([
            'accepted_terms_at' => $now,
            'accepted_privacy_at' => $now,
            'accepted_withdrawal_notice_at' => $now,
            'digital_content_waiver_at' => null,
            'legal_version' => config('app.legal_version', $now->toDateString()),
        ])->save();

        $payload = [
            'success_url' => $request->input('success_url'),
            'return_url' => $request->input('return_url'),
            'metadata' => [
                'checkout_session_id' => $session->id,
                'legal_version' => $session->legal_version,
                'accepted_terms' => true,
            ],
        ];

        $checkout = $this->paddleCheckout->createCheckout($tenant, $package, $payload);

        $session->forceFill([
            'paddle_checkout_id' => $checkout['id'] ?? $session->paddle_checkout_id,
            'provider_metadata' => array_merge($session->provider_metadata ?? [], array_filter([
                'paddle_checkout_id' => $checkout['id'] ?? null,
                'paddle_checkout_url' => $checkout['checkout_url'] ?? null,
                'paddle_expires_at' => $checkout['expires_at'] ?? null,
            ])),
        ])->save();

        return response()->json(array_merge($checkout, [
            'checkout_session_id' => $session->id,
        ]));
    }

    public function checkoutSessionStatus(CheckoutSessionStatusRequest $request, CheckoutSession $session): JsonResponse
    {
        $history = $session->status_history ?? [];
        $reason = null;

        foreach (array_reverse($history) as $entry) {
            if (($entry['status'] ?? null) === $session->status) {
                $reason = $entry['reason'] ?? null;
                break;
            }
        }

        $checkoutUrl = data_get($session->provider_metadata ?? [], 'paddle_checkout_url');

        return response()->json([
            'status' => $session->status,
            'completed_at' => optional($session->completed_at)->toIso8601String(),
            'reason' => $reason,
            'checkout_url' => is_string($checkoutUrl) ? $checkoutUrl : null,
        ]);
    }

    private function handleFreePurchase(Request $request, Package $package, $tenant): JsonResponse
    {
        DB::transaction(function () use ($request, $package, $tenant) {
            $purchaseData = [
                'tenant_id' => $tenant->id,
                'event_id' => $request->event_id,
                'package_id' => $package->id,
                'provider' => 'free',
                'provider_id' => 'free',
                'price' => $package->price,
                'type' => $request->type,
                'metadata' => json_encode([
                    'note' => 'Free package assigned',
                    'ip' => $request->ip(),
                ]),
            ];

            PackagePurchase::create($purchaseData);

            if ($request->event_id) {
                // Assign to event
                \App\Models\EventPackage::create([
                    'event_id' => $request->event_id,
                    'package_id' => $package->id,
                    'price' => $package->price,
                    'purchased_at' => now(),
                ]);
            } else {
                // Partner / reseller Event-Kontingent package
                \App\Models\TenantPackage::create([
                    'tenant_id' => $tenant->id,
                    'package_id' => $package->id,
                    'price' => $package->price,
                    'purchased_at' => now(),
                    'expires_at' => null,
                    'active' => true,
                ]);
            }
        });

        return response()->json([
            'message' => 'Free package assigned successfully.',
            'purchase' => ['package' => $package->name, 'type' => $request->type],
        ], 201);
    }

    private function handlePaidPurchase(Request $request, Package $package, $tenant): JsonResponse
    {
        if (! $package->paddle_price_id) {
            throw ValidationException::withMessages(['package_id' => 'Package is not linked to a Paddle price.']);
        }

        $checkout = $this->paddleCheckout->createCheckout($tenant, $package, [
            'success_url' => $request->input('success_url'),
            'return_url' => $request->input('return_url'),
            'metadata' => array_filter([
                'type' => $request->input('type'),
                'event_id' => $request->input('event_id'),
            ]),
        ]);

        return response()->json($checkout);
    }
}
