<x-layouts.guest :title="__('Create Account')">
    <div>
        <h2 class="text-2xl font-bold text-ink">{{ __('Create your account') }}</h2>
        <p class="mt-1 text-sm text-muted">{{ __('Start automating your communication with AI') }}</p>
    </div>

    <form method="POST" action="{{ route('register') }}" class="mt-6 space-y-4"
          x-data="{ submitting: false }" @submit="submitting = true">
        @csrf

        {{-- Name --}}
        <div>
            <label for="name" class="block text-sm font-medium text-muted mb-1">{{ __('Full name') }}</label>
            <input id="name" type="text" name="name" value="{{ old('name') }}" required autofocus
                   autocomplete="name"
                   class="input @error('name') !border-danger @enderror"
                   placeholder="{{ __('John Doe') }}">
            @error('name') <p class="mt-1 text-xs text-danger">{{ $message }}</p> @enderror
        </div>

        {{-- Email --}}
        <div>
            <label for="email" class="block text-sm font-medium text-muted mb-1">{{ __('Email address') }}</label>
            <input id="email" type="email" name="email" value="{{ old('email') }}" required
                   autocomplete="email"
                   class="input @error('email') !border-danger @enderror"
                   placeholder="{{ __('you@company.com') }}">
            @error('email') <p class="mt-1 text-xs text-danger">{{ $message }}</p> @enderror
        </div>

        {{-- Password with strength indicator --}}
        <div x-data="{
            show: false,
            password: '',
            get strength() {
                let s = 0;
                if (this.password.length >= 10) s++;
                if (/[A-Z]/.test(this.password)) s++;
                if (/[a-z]/.test(this.password)) s++;
                if (/[0-9]/.test(this.password)) s++;
                if (/[^A-Za-z0-9]/.test(this.password)) s++;
                return s;
            },
            get strengthLabel() {
                if (!this.password) return '';
                if (this.strength <= 2) return 'Weak';
                if (this.strength <= 3) return 'Fair';
                if (this.strength <= 4) return 'Good';
                return 'Strong';
            },
            get strengthColor() {
                if (this.strength <= 2) return 'bg-danger/100';
                if (this.strength <= 3) return 'bg-warning/100';
                if (this.strength <= 4) return 'bg-info/100';
                return 'bg-success/100';
            }
        }">
            <label for="password" class="block text-sm font-medium text-muted mb-1">{{ __('Password') }}</label>
            <div class="relative">
                <input id="password" :type="show ? 'text' : 'password'" name="password" required
                       autocomplete="new-password"
                       x-model="password"
                       class="input pr-10 @error('password') !border-danger @enderror"
                       placeholder="{{ __('Min 10 chars, uppercase, number, symbol') }}">
                <button type="button" @click="show = !show" class="absolute right-3 top-1/2 -translate-y-1/2 text-muted hover:text-ink">
                    <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"/><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z"/></svg>
                </button>
            </div>
            <div x-show="password.length > 0" x-transition class="mt-1.5">
                <div class="flex gap-1 mb-0.5">
                    <template x-for="i in 5">
                        <div class="h-1 flex-1 rounded-full transition-colors" :class="i <= strength ? strengthColor : 'bg-gray-200'"></div>
                    </template>
                </div>
                <p class="text-xs" :class="{
                    'text-danger': strength <= 2,
                    'text-yellow-600': strength === 3,
                    'text-blue-600': strength === 4,
                    'text-success': strength === 5
                }" x-text="strengthLabel"></p>
            </div>
            @error('password') <p class="mt-1 text-xs text-danger">{{ $message }}</p> @enderror
        </div>

        {{-- Confirm password --}}
        <div>
            <label for="password_confirmation" class="block text-sm font-medium text-muted mb-1">{{ __('Confirm password') }}</label>
            <input id="password_confirmation" type="password" name="password_confirmation" required
                   autocomplete="new-password"
                   class="input"
                   placeholder="{{ __('Confirm your password') }}">
        </div>

        {{-- Terms --}}
        <div class="flex items-start">
            <input id="terms" type="checkbox" name="terms" required class="w-4 h-4 mt-0.5 rounded border-border text-brand focus:ring-brand">
            <label for="terms" class="ml-2 text-sm text-muted">{{ __('I agree to the') }} <a href="{{ url('/terms') }}" target="_blank" class="text-brand hover:underline">{{ __('Terms') }}</a> {{ __('and') }} <a href="{{ url('/privacy') }}" target="_blank" class="text-brand hover:underline">{{ __('Privacy Policy') }}</a></label>
        </div>
        @error('terms') <p class="text-xs text-danger">{{ $message }}</p> @enderror

        {{-- Submit --}}
        <button type="submit" :disabled="submitting"
                class="btn-primary w-full py-2.5"
                :class="submitting && 'opacity-75 cursor-not-allowed'">
            <span x-show="!submitting">{{ __('Create Account') }}</span>
            <span x-show="submitting" x-cloak class="inline-flex items-center justify-center gap-2">
                <svg class="w-4 h-4 animate-spin" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24"><circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"/><path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4z"/></svg>
                {{ __('Creating account...') }}
            </span>
        </button>

        @php
            // Mirror the login page gating — master switch + per-provider
            // toggles. Keeps the register form in sync with /login so
            // disabling GitHub in admin hides it on BOTH places.
            $__socialEnabled    = \App\Models\SystemSetting::get('social_login_enabled',         'true')  === 'true';
            $__googleEnabled    = \App\Models\SystemSetting::get('integration_google_enabled',    'false') === 'true';
            $__microsoftEnabled = \App\Models\SystemSetting::get('integration_microsoft_enabled', 'false') === 'true';
            $__githubEnabled    = \App\Models\SystemSetting::get('integration_github_enabled',    'false') === 'true';
            $__facebookEnabled  = \App\Models\SystemSetting::get('integration_facebook_enabled',  'false') === 'true';
            $__anySocial        = $__socialEnabled && ($__googleEnabled || $__microsoftEnabled || $__githubEnabled || $__facebookEnabled);
        @endphp

        @if($__anySocial)
        {{-- Divider --}}
        <div class="relative my-4">
            <div class="absolute inset-0 flex items-center"><div class="w-full border-t border-border"></div></div>
            <div class="relative flex justify-center text-xs"><span class="px-3 bg-surface-2 text-muted">{{ __('Or sign up with') }}</span></div>
        </div>

        @if($__googleEnabled || $__microsoftEnabled)
        {{-- Social — 1 or 2 in a row depending on which providers are enabled --}}
        <div class="grid {{ $__googleEnabled && $__microsoftEnabled ? 'grid-cols-2' : 'grid-cols-1' }} gap-3">
            @if($__googleEnabled)
            <a href="{{ url('/auth/google/redirect') }}" class="flex items-center justify-center gap-2 py-2.5 px-3 bg-surface-2 border border-border rounded-xl text-sm font-medium text-ink hover:bg-surface transition-colors">
                <svg class="w-4 h-4" viewBox="0 0 24 24"><path d="M22.56 12.25c0-.78-.07-1.53-.2-2.25H12v4.26h5.92a5.06 5.06 0 01-2.2 3.32v2.77h3.57c2.08-1.92 3.28-4.74 3.28-8.1z" fill="#4285F4"/><path d="M12 23c2.97 0 5.46-.98 7.28-2.66l-3.57-2.77c-.98.66-2.23 1.06-3.71 1.06-2.86 0-5.29-1.93-6.16-4.53H2.18v2.84C3.99 20.53 7.7 23 12 23z" fill="#34A853"/><path d="M5.84 14.09c-.22-.66-.35-1.36-.35-2.09s.13-1.43.35-2.09V7.07H2.18C1.43 8.55 1 10.22 1 12s.43 3.45 1.18 4.93l2.85-2.22.81-.62z" fill="#FBBC05"/><path d="M12 5.38c1.62 0 3.06.56 4.21 1.64l3.15-3.15C17.45 2.09 14.97 1 12 1 7.7 1 3.99 3.47 2.18 7.07l3.66 2.84c.87-2.6 3.3-4.53 6.16-4.53z" fill="#EA4335"/></svg>
                {{ __('Google') }}
            </a>
            @endif
            @if($__microsoftEnabled)
            <a href="{{ url('/auth/microsoft/redirect') }}" class="flex items-center justify-center gap-2 py-2.5 px-3 bg-surface-2 border border-border rounded-xl text-sm font-medium text-ink hover:bg-surface transition-colors">
                <svg class="w-4 h-4" viewBox="0 0 24 24"><rect x="1" y="1" width="10" height="10" fill="#F25022"/><rect x="13" y="1" width="10" height="10" fill="#7FBA00"/><rect x="1" y="13" width="10" height="10" fill="#00A4EF"/><rect x="13" y="13" width="10" height="10" fill="#FFB900"/></svg>
                {{ __('Microsoft') }}
            </a>
            @endif
        </div>
        @endif
        @if($__githubEnabled)
        <a href="{{ url('/auth/github/redirect') }}" class="flex items-center justify-center gap-2 w-full py-2.5 px-3 bg-surface-2 border border-border rounded-xl text-sm font-medium text-ink hover:bg-surface transition-colors">
            <svg class="w-4 h-4" fill="currentColor" viewBox="0 0 24 24"><path d="M12 0c-6.626 0-12 5.373-12 12 0 5.302 3.438 9.8 8.207 11.387.599.111.793-.261.793-.577v-2.234c-3.338.726-4.033-1.416-4.033-1.416-.546-1.387-1.333-1.756-1.333-1.756-1.089-.745.083-.729.083-.729 1.205.084 1.839 1.237 1.839 1.237 1.07 1.834 2.807 1.304 3.492.997.107-.775.418-1.305.762-1.604-2.665-.305-5.467-1.334-5.467-5.931 0-1.311.469-2.381 1.236-3.221-.124-.303-.535-1.524.117-3.176 0 0 1.008-.322 3.301 1.23.957-.266 1.983-.399 3.003-.404 1.02.005 2.047.138 3.006.404 2.291-1.552 3.297-1.23 3.297-1.23.653 1.653.242 2.874.118 3.176.77.84 1.235 1.911 1.235 3.221 0 4.609-2.807 5.624-5.479 5.921.43.372.823 1.102.823 2.222v3.293c0 .319.192.694.801.576 4.765-1.589 8.199-6.086 8.199-11.386 0-6.627-5.373-12-12-12z"/></svg>
            {{ __('Continue with GitHub') }}
        </a>
        @endif
        @if($__facebookEnabled)
        <a href="{{ url('/auth/facebook/redirect') }}" class="flex items-center justify-center gap-2 w-full py-2.5 px-3 bg-surface-2 border border-border rounded-xl text-sm font-medium text-ink hover:bg-surface transition-colors mt-3">
            <svg class="w-4 h-4" viewBox="0 0 24 24" fill="#1877F2"><path d="M24 12.073c0-6.627-5.373-12-12-12s-12 5.373-12 12c0 5.99 4.388 10.954 10.125 11.854v-8.385H7.078v-3.47h3.047V9.43c0-3.007 1.792-4.669 4.533-4.669 1.312 0 2.686.235 2.686.235v2.953H15.83c-1.491 0-1.956.925-1.956 1.874v2.25h3.328l-.532 3.47h-2.796v8.385C19.612 23.027 24 18.062 24 12.073z"/></svg>
            {{ __('Continue with Facebook') }}
        </a>
        @endif
        @endif

        {{-- Login link --}}
        <p class="text-center text-sm text-muted mt-4">
            {{ __('Already have an account?') }} <a href="{{ route('login') }}" class="text-brand font-semibold hover:text-brand-strong">{{ __('Log in') }}</a>
        </p>
    </form>
</x-layouts.guest>
