<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class RedditAccountsController extends Controller
{
    /**
     * Display the Buy Reddit Accounts USA page
     *
     * @return \Illuminate\View\View
     */
    public function index()
    {
        // SEO Meta Data
        $seoData = [
            'title' => 'Buy Reddit Accounts USA | Aged, High Karma American Accounts',
            'description' => 'Purchase authentic USA Reddit accounts with verified emails, aged history, and high karma. Instant delivery. Target American audiences effectively. 100% satisfaction guaranteed.',
            'keywords' => 'buy reddit accounts usa, reddit accounts for sale, aged reddit accounts, high karma reddit accounts, usa reddit accounts',
            'og_image' => asset('images/reddit-accounts-usa-og.jpg'),
            'canonical' => route('reddit.accounts.usa'),
        ];

        // Pricing Packages
        $packages = [
            'starter' => [
                'name' => 'Starter Pack',
                'price' => 49,
                'subtitle' => 'Perfect for testing the waters',
                'features' => [
                    '1-2 Year Old Account',
                    '500-2,000 Karma',
                    'USA IP History',
                    'Verified Email',
                    'Post History Included',
                    'Instant Delivery',
                    '30-Day Guarantee'
                ]
            ],
            'professional' => [
                'name' => 'Professional Pack',
                'price' => 89,
                'subtitle' => 'Most Popular Choice',
                'badge' => 'BEST VALUE',
                'features' => [
                    '2-4 Year Old Account',
                    '2,000-10,000 Karma',
                    'USA IP History',
                    'Verified Email',
                    'Diverse Post History',
                    'Multiple Subreddit Activity',
                    'Instant Delivery',
                    '30-Day Guarantee'
                ]
            ],
            'elite' => [
                'name' => 'Elite Pack',
                'price' => 199,
                'subtitle' => 'Maximum Authority',
                'features' => [
                    '4-5+ Year Old Account',
                    '10,000-50,000+ Karma',
                    'USA IP History',
                    'Verified Email',
                    'Extensive Post History',
                    'Premium Subreddit Access',
                    'Moderator Experience (some)',
                    'Instant Delivery',
                    '30-Day Guarantee'
                ]
            ],
            'bulk' => [
                'name' => 'Custom Bulk Orders',
                'price' => 'Starting at $39',
                'subtitle' => 'For agencies & businesses',
                'features' => [
                    '10+ Accounts',
                    'Custom Karma Requirements',
                    'Specific Subreddit History',
                    'Age Preferences',
                    'Dedicated Account Manager',
                    'Priority Support',
                    'Volume Discounts up to 40%'
                ]
            ]
        ];

        // Statistics
        $stats = [
            'customers' => '10,000+',
            'accounts_sold' => '50,000+',
            'rating' => '4.9/5',
            'years' => '5+'
        ];

        // Testimonials
        $testimonials = [
            [
                'text' => 'I bought 5 USA accounts for my e-commerce store and saw immediate results. Posted in r/deals and got 10,000+ views in 24 hours. The accounts are legit and the karma was exactly as advertised.',
                'author' => 'Michael R.',
                'role' => 'E-commerce Owner',
                'location' => 'Austin, TX',
                'rating' => 5
            ],
            [
                'text' => 'As a digital marketing agency, we needed reliable Reddit accounts for multiple clients. The bulk package was perfect—great pricing, instant delivery, and zero issues.',
                'author' => 'Sarah K.',
                'role' => 'Marketing Director',
                'location' => 'New York, NY',
                'rating' => 5
            ],
            [
                'text' => 'Skeptical at first, but the quality exceeded expectations. The account had a 3-year history, 8K karma, and a diverse post background. Worth every penny.',
                'author' => 'James T.',
                'role' => 'Content Creator',
                'location' => 'Los Angeles, CA',
                'rating' => 5
            ]
        ];

        // FAQs
        $faqs = [
            [
                'question' => 'Are these real Reddit accounts?',
                'answer' => 'Yes, 100%. These are genuine Reddit accounts that have been organically grown over years. They have real post histories, authentic karma, and legitimate engagement patterns. We never sell fake or bot accounts.'
            ],
            [
                'question' => 'Why specifically USA accounts?',
                'answer' => 'USA-based accounts perform significantly better when targeting American audiences. They have US IP histories, post during American hours, and engage in US-centric subreddits—making them appear more authentic to both users and Reddit\'s algorithms.'
            ],
            [
                'question' => 'How quickly will I receive my account?',
                'answer' => 'Instantly! Most orders are delivered within 5-10 minutes of purchase confirmation. You\'ll receive an email with login credentials and full access details.'
            ],
            [
                'question' => 'Can I change the password and email?',
                'answer' => 'Absolutely. You\'ll receive complete account access, including the current email address. You can (and should) change both the password and email to secure the account under your control.'
            ],
            [
                'question' => 'What if the account gets banned?',
                'answer' => 'If an account is suspended within 30 days due to its history (not your actions), we\'ll replace it free of charge. However, if a ban occurs due to your usage violating Reddit\'s rules, we cannot offer replacements.'
            ],
            [
                'question' => 'Do you offer refunds?',
                'answer' => 'Yes, we have a 30-day money-back guarantee. If you\'re not satisfied for any reason, contact us within 30 days for a full refund.'
            ]
        ];

        return view('reddit-accounts.buy-usa', compact(
            'seoData',
            'packages',
            'stats',
            'testimonials',
            'faqs'
        ));
    }

    /**
     * Handle checkout for specific package
     *
     * @param Request $request
     * @param string $package
     * @return \Illuminate\View\View
     */
    public function checkout(Request $request, $package)
    {
        // Validate package
        $validPackages = ['starter', 'professional', 'elite'];
        
        if (!in_array($package, $validPackages)) {
            abort(404);
        }

        // Package details
        $packageDetails = $this->getPackageDetails($package);

        return view('reddit-accounts.checkout', compact('packageDetails'));
    }

    /**
     * Get package details by slug
     *
     * @param string $package
     * @return array
     */
    private function getPackageDetails($package)
    {
        $packages = [
            'starter' => [
                'name' => 'Starter Pack',
                'price' => 49,
                'description' => '1-2 Year Old Account with 500-2,000 Karma'
            ],
            'professional' => [
                'name' => 'Professional Pack',
                'price' => 89,
                'description' => '2-4 Year Old Account with 2,000-10,000 Karma'
            ],
            'elite' => [
                'name' => 'Elite Pack',
                'price' => 199,
                'description' => '4-5+ Year Old Account with 10,000-50,000+ Karma'
            ]
        ];

        return $packages[$package] ?? null;
    }

    /**
     * Process the order
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function processOrder(Request $request)
    {
        $validated = $request->validate([
            'package' => 'required|in:starter,professional,elite',
            'quantity' => 'required|integer|min:1|max:100',
            'email' => 'required|email',
            'payment_method' => 'required|in:card,paypal,crypto'
        ]);

        // Process payment logic here
        // This would integrate with Stripe, PayPal, or crypto payment gateway

        // Send confirmation email
        // Mail::to($validated['email'])->send(new OrderConfirmation($orderDetails));

        return response()->json([
            'success' => true,
            'message' => 'Order processed successfully. You will receive your account details within 5-10 minutes.',
            'order_id' => 'ORD-' . strtoupper(uniqid())
        ]);
    }

    /**
     * Show contact form for bulk orders
     *
     * @return \Illuminate\View\View
     */
    public function bulkInquiry()
    {
        return view('reddit-accounts.bulk-inquiry');
    }

    /**
     * Submit bulk inquiry
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function submitBulkInquiry(Request $request)
    {
        $validated = $request->validate([
            'name' => 'required|string|max:255',
            'email' => 'required|email',
            'company' => 'nullable|string|max:255',
            'quantity' => 'required|integer|min:10',
            'requirements' => 'required|string|max:1000',
            'phone' => 'nullable|string|max:20'
        ]);

        // Send inquiry to sales team
        // Mail::to('sales@yoursite.com')->send(new BulkInquiry($validated));

        return response()->json([
            'success' => true,
            'message' => 'Thank you for your inquiry. Our team will contact you within 24 hours.'
        ]);
    }
}