/**
 * Auth Page Service
 *
 * Processes URL parameters for auth pages and provides structured data.
 * This is a composition service, not an API service.
 */

import { Result } from '@/lib/contracts/Result';
import { DomainError, Service } from '@/lib/contracts/services/Service';
import { LoginPageDTO } from './types/LoginPageDTO';
import { ForgotPasswordPageDTO } from './types/ForgotPasswordPageDTO';
import { ResetPasswordPageDTO } from './types/ResetPasswordPageDTO';
import { SignupPageDTO } from './types/SignupPageDTO';
import { AuthPageParams } from './AuthPageParams';

export class AuthPageService implements Service {
  async processLoginParams(params: AuthPageParams): Promise<Result<LoginPageDTO, DomainError>> {
    try {
      const returnTo = params.returnTo ?? '/dashboard';
      const hasInsufficientPermissions = params.returnTo !== null;

      return Result.ok({
        returnTo,
        hasInsufficientPermissions,
      });
    } catch (error) {
      return Result.err({ type: 'unknown', message: 'Failed to process login parameters' });
    }
  }

  async processForgotPasswordParams(params: AuthPageParams): Promise<Result<ForgotPasswordPageDTO, DomainError>> {
    try {
      const returnTo = params.returnTo ?? '/auth/login';
      return Result.ok({ returnTo });
    } catch (error) {
      return Result.err({ type: 'unknown', message: 'Failed to process forgot password parameters' });
    }
  }

  async processResetPasswordParams(params: AuthPageParams): Promise<Result<ResetPasswordPageDTO, DomainError>> {
    try {
      const token = params.token;
      if (!token) {
        return Result.err({ type: 'validation', message: 'Missing reset token' });
      }
      const returnTo = params.returnTo ?? '/auth/login';
      return Result.ok({ token, returnTo });
    } catch (error) {
      return Result.err({ type: 'unknown', message: 'Failed to process reset password parameters' });
    }
  }

  async processSignupParams(params: AuthPageParams): Promise<Result<SignupPageDTO, DomainError>> {
    try {
      const returnTo = params.returnTo ?? '/onboarding';
      return Result.ok({ returnTo });
    } catch (error) {
      return Result.err({ type: 'unknown', message: 'Failed to process signup parameters' });
    }
  }
}
