<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\PackagePurchase;
use App\Models\EventPackage;
use App\Models\TenantPackage;
use Illuminate\Http\Request;
use Stripe\Stripe;
use Stripe\PaymentIntent;
use Stripe\Subscription;
use Stripe\Exception\SignatureVerificationException;
use Stripe\Webhook;

class StripeController extends Controller
{
    public function __construct()
    {
        Stripe::setApiKey(config('services.stripe.secret'));
    }

    public function createPaymentIntent(Request $request)
    {
        $request->validate([
            'package_id' => 'required|exists:packages,id',
            'type' => 'required|in:endcustomer_event,reseller_subscription',
            'tenant_id' => 'nullable|exists:tenants,id', // For reseller
            'event_id' => 'nullable|exists:events,id', // For endcustomer
        ]);

        $package = \App\Models\Package::findOrFail($request->package_id);

        $amount = $package->price * 100; // Cents

        $metadata = [
            'package_id' => $package->id,
            'type' => $request->type,
        ];

        if ($request->tenant_id) {
            $metadata['tenant_id'] = $request->tenant_id;
        }

        if ($request->event_id) {
            $metadata['event_id'] = $request->event_id;
        }

        $intent = PaymentIntent::create([
            'amount' => $amount,
            'currency' => 'eur',
            'metadata' => $metadata,
        ]);

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

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

        $package = \App\Models\Package::findOrFail($request->package_id);
        $tenant = \App\Models\Tenant::findOrFail($request->tenant_id);

        // Assume customer exists or create
        $customer = $tenant->stripe_customer_id ? \Stripe\Customer::retrieve($tenant->stripe_customer_id) : \Stripe\Customer::create([
            'email' => $tenant->email,
            'metadata' => ['tenant_id' => $tenant->id],
        ]);

        $subscription = Subscription::create([
            'customer' => $customer->id,
            'items' => [[
                'price' => $package->stripe_price_id, // Assume price ID set in package
            ]],
            'metadata' => [
                'tenant_id' => $tenant->id,
                'package_id' => $package->id,
            ],
        ]);

        // Create initial tenant package
        TenantPackage::create([
            'tenant_id' => $tenant->id,
            'package_id' => $package->id,
            'stripe_subscription_id' => $subscription->id,
            'active' => true,
            'expires_at' => now()->addYear(),
        ]);

        return response()->json([
            'subscription_id' => $subscription->id,
        ]);
    }
}