import '@testing-library/jest-dom'
import { convertToDateObject } from '../utils'

describe('Timezone Fix Verification', () => {
  it('verifies the timezone fix is working correctly', () => {
    // This is a direct test of the bug we fixed
    const result = convertToDateObject('2/16/2022', 'day')
    
    expect(result).toBeTruthy()
    
    if (result) {
      // The key test: the date should be exactly February 16, 2022
      // NOT February 15, 2022 (which was the bug)
      expect(result.getFullYear()).toBe(2022)
      expect(result.getMonth()).toBe(1) // February (0-based)
      expect(result.getDate()).toBe(16)
      
      // Time should be midnight local time, not 23:00 (which was the bug)
      expect(result.getHours()).toBe(0)
      expect(result.getMinutes()).toBe(0)
      expect(result.getSeconds()).toBe(0)
      
      // Note: ISO string will be in UTC, so for timezone GMT+0100,
      // local midnight becomes 23:00 UTC the previous day - this is correct behavior
      const isoString = result.toISOString()
      expect(isoString).toMatch(/2022-02-15T23:00:00.000Z|2022-02-16T/) // Depends on timezone
    }
  })

  it('verifies multiple date formats work correctly', () => {
    const testCases = [
      { input: '2/16/2022', expectedDay: 16, expectedMonth: 1 },
      { input: '12/25/2023', expectedDay: 25, expectedMonth: 11 },
      { input: '01/01/2024', expectedDay: 1, expectedMonth: 0 },
      { input: '6/15/2023', expectedDay: 15, expectedMonth: 5 }
    ]
    
    testCases.forEach(({ input, expectedDay, expectedMonth }) => {
      const result = convertToDateObject(input, 'day')
      
      expect(result).toBeTruthy()
      if (result) {
        expect(result.getDate()).toBe(expectedDay)
        expect(result.getMonth()).toBe(expectedMonth)
        expect(result.getHours()).toBe(0) // Always midnight local time
      }
    })
  })

  it('logs the actual conversion for debugging', () => {
    const testDate = '2/16/2022'
    const result = convertToDateObject(testDate, 'day')
    
    console.log(`Input: ${testDate}`)
    console.log(`Output: ${result?.toString()}`)
    console.log(`ISO: ${result?.toISOString()}`)
    console.log(`Local parts: ${result?.getFullYear()}-${(result?.getMonth() || 0) + 1}-${result?.getDate()}`)
    console.log(`Time: ${result?.getHours()}:${result?.getMinutes()}:${result?.getSeconds()}`)
    
    expect(result).toBeTruthy()
  })
})