import { readFileSync } from 'node:fs'
import { resolve } from 'node:path'
import { describe, expect, it } from 'vitest'

function readRepoFile(relativePath: string): string {
  return readFileSync(resolve(__dirname, '../../..', relativePath), 'utf8')
}

describe('zync-www marketing/auth contract (spec 34)', () => {
  it('signup form posts businessName and redirects straight to onboarding', () => {
    const source = readRepoFile('apps/zync-www/src/components/auth/SignupForm.tsx')

    expect(source).toContain("businessName: data.businessName")
    expect(source).toContain('window.location.href = `${APP_URL}/onboarding`')
    expect(source).not.toContain('בדוק את תיבת הדואר')
  })

  it('reset password forms use the spec endpoints and labels', () => {
    const requestSource = readRepoFile('apps/zync-www/src/components/auth/ResetRequestForm.tsx')
    const confirmSource = readRepoFile('apps/zync-www/src/components/auth/ResetConfirmForm.tsx')

    expect(requestSource).toContain("apiFetch('/api/auth/reset-password'")
    expect(requestSource).toContain('שליחת קישור')
    expect(confirmSource).toContain("apiFetch('/api/auth/reset-password/confirm'")
    expect(confirmSource).toContain('עדכון סיסמה')
  })

  it('invite accept form uses fullName and the spec button labels', () => {
    const source = readRepoFile('apps/zync-www/src/components/auth/InviteAcceptForm.tsx')

    expect(source).toContain('fullName: data.fullName')
    expect(source).toContain('הצטרפות לצוות')
    expect(source).toContain('אישור הצטרפות')
  })

  it('auth cookies are shared on .zync.is with SameSite=Lax', () => {
    const source = readRepoFile('apps/zync-api/src/lib/cookies.ts')

    expect(source).toContain("sameSite: 'Lax'")
    expect(source).toContain("domain: '.zync.is'")
  })

  it('login route does not block unverified users from receiving a session', () => {
    const source = readRepoFile('apps/zync-api/src/routes/auth/login.ts')

    expect(source).not.toContain('if (!user.emailVerifiedAt)')
  })
})
