<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Package;
use App\Models\PackagePurchase;
use App\Models\TenantPackage;
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) {}

    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:stripe,paddle',
            'event_id' => 'nullable|exists:events,id', // For endcustomer
        ]);

        $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 createPaymentIntent(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.']);
        }

        \Stripe\Stripe::setApiKey(config('services.stripe.secret'));

        $paymentIntent = \Stripe\PaymentIntent::create([
            'amount' => $package->price * 100,
            'currency' => 'eur',
            'metadata' => [
                'tenant_id' => $tenant->id,
                'package_id' => $package->id,
                'type' => 'endcustomer_event',
            ],
        ]);

        return response()->json([
            'client_secret' => $paymentIntent->client_secret,
        ]);
    }

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

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

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

        $provider = $request->has('paddle_transaction_id') ? 'paddle' : 'stripe';

        DB::transaction(function () use ($request, $package, $tenant, $provider) {
            PackagePurchase::create([
                'tenant_id' => $tenant->id,
                'package_id' => $package->id,
                'provider' => $provider,
                'provider_id' => $request->input($provider === 'paddle' ? 'paddle_transaction_id' : 'payment_method_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');

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

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

        $payload = [
            'success_url' => $request->input('success_url'),
            'return_url' => $request->input('return_url'),
        ];

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

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

    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 {
                // Reseller subscription
                \App\Models\TenantPackage::create([
                    'tenant_id' => $tenant->id,
                    'package_id' => $package->id,
                    'price' => $package->price,
                    'purchased_at' => now(),
                    'expires_at' => now()->addYear(),
                    '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
    {
        $type = $request->type;

        if ($type === 'reseller_subscription') {
            $response = (new StripeController)->createSubscription($request);

            return $response;
        } else {
            $response = (new StripeController)->createPaymentIntent($request);

            return $response;
        }
    }
}
