<?php

namespace App\Support;

class SocializeHelper
{
    public function driver($name)
    {
        $driver = strtolower($name);

        if (! in_array($driver, ['google', 'facebook', 'twitter'], true)) {
            throw new \InvalidArgumentException("Unsupported social driver: {$driver}");
        }

        return new class($driver) {
            protected $driver;

            public function __construct($driver)
            {
                $this->driver = $driver;
            }

            public function redirect()
            {
                $config = config('services.' . $this->driver, []);

                if (empty($config['client_id']) || empty($config['client_secret'])) {
                    throw new \RuntimeException('Social login is not configured for ' . $this->driver);
                }

                return redirect()->away(sprintf(
                    '%s?client_id=%s&redirect_uri=%s&response_type=code&scope=%s',
                    'https://accounts.google.com/o/oauth2/auth',
                    urlencode($config['client_id']),
                    urlencode($config['redirect']),
                    urlencode($config['scope'] ?? 'openid email profile')
                ));
            }

            public function user()
            {
                throw new \BadMethodCallException('Social login callback handling is not implemented in the compatibility layer. Integrate with a real OAuth provider before production use.');
            }
        };
    }
}
