<?php

namespace App\Livewire\Dashboard;

use Livewire\Component;
use Session;
use function Pest\Laravel\withoutMockingConsoleOutput;

class BulkGmail extends Component
{
    public $email;
    public int $quantity = 10;
    public $bulkEmails = [];

    public function mount() {
        $subscriptionCheck = auth()->user()->subscribedToProduct(config('app.plans')[0]['product_id']);
        Session::put('isSubscribed', $subscriptionCheck);
    }

    public function generateBulk(): void
    {
        $this->validate([
            'email' => [
                'required',
                'email',
                'regex:/^[^@\s]+@gmail\.com$/i',
            ],
            'quantity' => 'required|integer|min:10|max:500',
        ]);

        if (count($this->bulkEmails) > 0) {
            $this->bulkEmails = [];
        }

        $this->bulkEmails = $this->generateDotVariants(explode("@", $this->email)[0], $this->quantity);
    }

    public function downloadBulk()
    {
        // Ensure there's something to download
        if (empty($this->bulkEmails) || !is_array($this->bulkEmails)) {
            return;
        }

        $filename = 'bulk_gmails_' . now()->format('Ymd_His') . '.txt';
        $content = implode(PHP_EOL, $this->bulkEmails);

        return response()->streamDownload(function () use ($content) {
            echo $content;
        }, $filename);
    }

    private function generateDotVariants($uname, $quantity)
    {
        $length = strlen($uname);
        $positions = range(1, $length - 1);
        $results = [];

        // From 1 dot up to max possible
        for ($dotCount = 1; $dotCount < $length; $dotCount++) {
            $combinations = $this->combinations($positions, $dotCount);

            // Reverse each combination for right-to-left priority
            foreach (array_reverse($combinations) as $combo) {
                $dotted = '';
                $lastPos = 0;

                foreach ($combo as $pos) {
                    $dotted .= substr($uname, $lastPos, $pos - $lastPos) . '.';
                    $lastPos = $pos;
                }
                $dotted .= substr($uname, $lastPos);
                $results[] = $dotted . '@gmail.com';

                if (count($results) >= $quantity) {
                    return $results;
                }
            }
        }

        return $results;
    }

    private function combinations(array $items, int $size): array
    {
        if ($size === 0) {
            return [[]];
        }

        if (empty($items)) {
            return [];
        }

        $combinations = [];
        $head = $items[0];
        $tail = array_slice($items, 1);

        foreach ($this->combinations($tail, $size - 1) as $c) {
            array_unshift($c, $head);
            $combinations[] = $c;
        }

        foreach ($this->combinations($tail, $size) as $c) {
            $combinations[] = $c;
        }

        return $combinations;
    }

    public function render()
    {
        if (Session::get('isSubscribed')) {
            return view('livewire.dashboard.bulk-gmail')->layout('components.layouts.dashboard');
        } else {
            return view('livewire.dashboard.not-subscribed')->layout('components.layouts.dashboard');
        }
    }
}
