import { describe, it, expect } from 'vitest';
import {
  listingBasicSchema,
  listingLocationSchema,
  listingPricingSchema,
  createListingSchema,
  TRANSACTION_TYPES,
  PROPERTY_TYPES,
  DIRECTIONS,
  LISTING_STATUSES,
} from '../validations/listings';

describe('listingBasicSchema', () => {
  it('should accept valid basic info', () => {
    const result = listingBasicSchema.safeParse({
      transactionType: 'SALE',
      propertyType: 'APARTMENT',
      title: 'Căn hộ đẹp',
      description: 'Mô tả chi tiết về căn hộ',
    });
    expect(result.success).toBe(true);
  });

  it('should reject invalid transaction type', () => {
    const result = listingBasicSchema.safeParse({
      transactionType: 'INVALID',
      propertyType: 'APARTMENT',
      title: 'Valid title',
      description: 'Valid description',
    });
    expect(result.success).toBe(false);
  });

  it('should reject short title', () => {
    const result = listingBasicSchema.safeParse({
      transactionType: 'SALE',
      propertyType: 'HOUSE',
      title: 'Ab',
      description: 'Valid description',
    });
    expect(result.success).toBe(false);
  });

  it('should reject short description', () => {
    const result = listingBasicSchema.safeParse({
      transactionType: 'RENT',
      propertyType: 'VILLA',
      title: 'Valid title',
      description: 'Short',
    });
    expect(result.success).toBe(false);
  });

  it('should accept all valid property types', () => {
    for (const pt of PROPERTY_TYPES) {
      const result = listingBasicSchema.safeParse({
        transactionType: 'SALE',
        propertyType: pt.value,
        title: 'Valid title here',
        description: 'Valid description here',
      });
      expect(result.success).toBe(true);
    }
  });
});

describe('listingLocationSchema', () => {
  it('should accept valid location', () => {
    const result = listingLocationSchema.safeParse({
      address: '123 Đường ABC',
      ward: 'Phường 1',
      district: 'Quận 1',
      city: 'TP. Hồ Chí Minh',
    });
    expect(result.success).toBe(true);
  });

  it('should reject empty address', () => {
    const result = listingLocationSchema.safeParse({
      address: '',
      ward: 'Phường 1',
      district: 'Quận 1',
      city: 'TP. HCM',
    });
    expect(result.success).toBe(false);
  });

  it('should accept optional lat/lng', () => {
    const result = listingLocationSchema.safeParse({
      address: '123 Đường ABC',
      ward: 'Phường 1',
      district: 'Quận 1',
      city: 'Hà Nội',
      latitude: '21.0285',
      longitude: '105.8542',
    });
    expect(result.success).toBe(true);
  });
});

describe('listingPricingSchema', () => {
  it('should accept valid price', () => {
    const result = listingPricingSchema.safeParse({ priceVND: '5000000000' });
    expect(result.success).toBe(true);
  });

  it('should reject empty price', () => {
    const result = listingPricingSchema.safeParse({ priceVND: '' });
    expect(result.success).toBe(false);
  });

  it('should accept optional rent and commission', () => {
    const result = listingPricingSchema.safeParse({
      priceVND: '15000000',
      rentPriceMonthly: '15000000',
      commissionPct: '3',
    });
    expect(result.success).toBe(true);
  });
});

describe('createListingSchema (merged)', () => {
  const validListing = {
    transactionType: 'SALE' as const,
    propertyType: 'APARTMENT' as const,
    title: 'Căn hộ 2PN Quận 7',
    description: 'Căn hộ view sông, full nội thất',
    address: '123 Nguyễn Hữu Thọ',
    ward: 'Phường Tân Hưng',
    district: 'Quận 7',
    city: 'TP. Hồ Chí Minh',
    areaM2: '75',
    priceVND: '3500000000',
  };

  it('should accept a complete valid listing', () => {
    const result = createListingSchema.safeParse(validListing);
    expect(result.success).toBe(true);
  });

  it('should reject if basic fields missing', () => {
    const { title: _, ...noTitle } = validListing;
    const result = createListingSchema.safeParse(noTitle);
    expect(result.success).toBe(false);
  });
});

describe('constants', () => {
  it('should have 2 transaction types', () => {
    expect(TRANSACTION_TYPES).toHaveLength(2);
  });

  it('should have 6 property types', () => {
    expect(PROPERTY_TYPES).toHaveLength(6);
  });

  it('should have 8 directions', () => {
    expect(DIRECTIONS).toHaveLength(8);
  });

  it('should have 8 listing statuses', () => {
    expect(Object.keys(LISTING_STATUSES)).toHaveLength(8);
  });
});
