<?php

namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Controller;
use App\Models\ImmigoProQuotationAnswer;
use App\Models\ImmigoProSubscription;
use Illuminate\Http\Request;

class AdminImmigoProQuotationController extends Controller
{
    /**
     * Get quotation answers for a specific user (subscription)
     */
    public function getUserQuotationAnswers($subscriptionId)
    {
        try {
            // Get the subscription with user details
            $subscription = ImmigoProSubscription::with(['user'])
                ->findOrFail($subscriptionId);

            // Get all quotation answers for this user with question details
            $answers = ImmigoProQuotationAnswer::with(['question'])
                ->where('user_id', $subscription->user_id)
                ->get()
                ->map(function ($answer) {
                    return [
                        'id' => $answer->id,
                        'answer' => $answer->answer,
                        'question' => [
                            'id' => $answer->question->id,
                            'question_text' => $answer->question->question ?? $answer->question->question_text,
                            'question_type' => $answer->question->type,
                            'options' => $answer->question->options,
                        ],
                        'created_at' => $answer->created_at,
                        'updated_at' => $answer->updated_at,
                    ];
                });

            // Get payment details (installments)
            $payments = \DB::table('installment_payments')
                ->where('subscription_id', $subscription->id)
                ->orderBy('due_date', 'asc')
                ->get()
                ->map(function ($payment) {
                    return [
                        'id' => $payment->id,
                        'amount' => $payment->amount,
                        'status' => $payment->status,
                        'due_date' => $payment->due_date,
                        'paid_at' => $payment->paid_at,
                        'stripe_invoice_id' => $payment->stripe_invoice_id,
                    ];
                });

            return response()->json([
                'subscription' => [
                    'id' => $subscription->id,
                    'user_id' => $subscription->user_id,
                    'package_name' => $subscription->package_name,
                    'payment_plan' => $subscription->payment_plan,
                    'status' => $subscription->status,
                    'activated_at' => $subscription->activated_at,
                    'license_key' => $subscription->license_key,
                    'base_price' => $subscription->base_price,
                    'upfront_amount' => $subscription->upfront_amount,
                    'monthly_amount' => $subscription->monthly_amount,
                    'total_amount' => $subscription->total_amount,
                    'tax_amount' => $subscription->tax_amount,
                    'installments' => $subscription->installments,
                    'paid_installments' => $subscription->paid_installments,
                    'next_charge_date' => $subscription->next_charge_date,
                ],
                'user' => [
                    'id' => $subscription->user->id,
                    'first_name' => $subscription->user->first_name,
                    'last_name' => $subscription->user->last_name,
                    'email' => $subscription->user->email,
                    'phone' => $subscription->user->phone ?? null,
                ],
                'quotation_answers' => $answers,
                'payments' => $payments,
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'message' => 'Failed to fetch user quotation answers',
                'error' => $e->getMessage(),
            ], 500);
        }
    }
}
