<?php

namespace App\Filament\Pages\Auth;

use Filament\Auth\Pages\Login as BaseLogin;
use Filament\Schemas\Components\Component;

class Login extends BaseLogin
{
    public function getTitle(): string
    {
        return __('auth.login.title') ?: parent::getTitle();
    }

    public function getHeading(): string
    {
        return __('auth.login.title') ?: parent::getHeading();
    }

    protected function getEmailFormComponent(): Component
    {
        $component = parent::getEmailFormComponent();

        return $component
            ->label(__('auth.login.username_or_email') ?: $component->getLabel());
    }

    protected function getPasswordFormComponent(): Component
    {
        $component = parent::getPasswordFormComponent();

        return $component
            ->label(__('auth.login.password') ?: $component->getLabel());
    }

    protected function getRememberFormComponent(): Component
    {
        $component = parent::getRememberFormComponent();

        return $component
            ->label(__('auth.login.remember_me') ?: $component->getLabel());
    }

    protected function getCredentialsFromFormData(array $data): array
    {
        $identifier = $data['email'] ?? '';
        $password = $data['password'] ?? '';

        if (filter_var($identifier, FILTER_VALIDATE_EMAIL)) {
            return [
                'email' => $identifier,
                'password' => $password,
            ];
        }

        return [
            'username' => $identifier,
            'password' => $password,
        ];
    }
}
