import { describe, it, expect } from 'vitest'
import { propertyService } from '../propertyService'

describe('propertyService', () => {
  describe('getAll', () => {
    it('returns a ListResponse with data and meta', async () => {
      const result = await propertyService.getAll()
      expect(Array.isArray(result.data)).toBe(true)
      expect(result.data.length).toBeGreaterThan(0)
      expect(result.meta!.total).toBe(result.data.length)
      expect(result.meta!.hasMore).toBe(false)
    })

    it('each property has required fields', async () => {
      const { data } = await propertyService.getAll()
      for (const p of data) {
        expect(typeof p.id).toBe('string')
        expect(typeof p.title).toBe('string')
        expect(typeof p.areaSqm).toBe('number')
        expect(typeof p.resultType).toBe('string')
        expect(typeof p.location.city).toBe('string')
      }
    })
  })

  describe('getById', () => {
    it('returns a property for an existing id', async () => {
      const { data: all } = await propertyService.getAll()
      const id = all[0].id
      const result = await propertyService.getById(id)
      expect(result.data?.id).toBe(id)
    })

    it('returns null for an unknown id', async () => {
      const result = await propertyService.getById('no-such-property')
      expect(result.data).toBeNull()
    })
  })

  describe('getDashboardPropertiesSummary', () => {
    it('returns total and active counts', async () => {
      const summary = await propertyService.getDashboardPropertiesSummary()
      expect(typeof summary.total).toBe('number')
      expect(typeof summary.active).toBe('number')
      expect(summary.active).toBeLessThanOrEqual(summary.total)
    })

    it('active count only includes AVAILABLE_NOW or AVAILABLE_SOON', async () => {
      const { data: all } = await propertyService.getAll()
      const expectedActive = all.filter(
        p => p.availabilityStatus === 'AVAILABLE_NOW' || p.availabilityStatus === 'AVAILABLE_SOON',
      ).length
      const summary = await propertyService.getDashboardPropertiesSummary()
      expect(summary.active).toBe(expectedActive)
    })
  })

  describe('create', () => {
    it('creates a property and returns it', async () => {
      const input = {
        title: 'Test Lagerfläche',
        assetType: 'LOGISTICS' as const,
        resultType: 'VERIFIED_PORTFOLIO' as const,
        location: { city: 'Basel', country: 'CH' as const },
        address: { street: 'Teststr.', houseNumber: '1', postalCode: '4000', city: 'Basel', country: 'CH' },
        areaSqm: 500,
        rentPricePerSqm: 120,
        availabilityDate: '2025-01-01',
        availabilityStatus: 'AVAILABLE_NOW' as const,
        sourceType: 'DIRECT',
        confidenceScore: 0.8,
        dataQuality: { score: 0.8, missingCriticalFields: [], missingOptionalFields: [], freshness: 'FRESH' as const, warnings: [] },
        organizationId: 'org-test',
      }
      const result = await propertyService.create(input)
      expect(typeof result.data.id).toBe('string')
      expect(result.data.title).toBe('Test Lagerfläche')
    })
  })
})
