import { z } from 'zod'
import { createFileRoute, redirect } from '@tanstack/react-router'
import { useAuthStore } from '@/stores/auth-store'
import { SignIn } from '@/features/auth/sign-in'

const searchSchema = z.object({
  redirect: z.string().optional(),
})

export const Route = createFileRoute('/(auth)/sign-in')({
  beforeLoad: () => {
    const { accessToken, user } = useAuthStore.getState().auth
    if (accessToken) {
      throw redirect({
        to: user?.mustChangePassword ? '/settings/account' : '/',
        replace: true,
      })
    }
  },
  component: SignIn,
  validateSearch: searchSchema,
})
