import { describe, it, expect } from 'vitest';
import { CreateStoreSchema, UpdateStoreSchema, StoreQuerySchema } from './store.schemas.js';

describe('CreateStoreSchema', () => {
  it('accepts minimal valid input', () => {
    const result = CreateStoreSchema.safeParse({ name: 'Walgreens' });
    expect(result.success).toBe(true);
    if (result.success) {
      expect(result.data.tags).toEqual([]);
      expect(result.data.isActive).toBe(true);
    }
  });

  it('accepts full valid input', () => {
    const result = CreateStoreSchema.safeParse({
      name: 'CVS Pharmacy',
      address: '123 Main St',
      location: { lat: 40.7128, lng: -74.006 },
      url: 'https://cvs.com',
      notes: 'Open 24h',
      tags: ['pharmacy', 'retail'],
      isActive: true,
    });
    expect(result.success).toBe(true);
  });

  it('rejects empty name', () => {
    expect(CreateStoreSchema.safeParse({ name: '' }).success).toBe(false);
  });

  it('rejects invalid location lat', () => {
    expect(CreateStoreSchema.safeParse({ name: 'X', location: { lat: 91, lng: 0 } }).success).toBe(
      false,
    );
  });

  it('rejects invalid location lng', () => {
    expect(CreateStoreSchema.safeParse({ name: 'X', location: { lat: 0, lng: 181 } }).success).toBe(
      false,
    );
  });

  it('trims name whitespace', () => {
    const result = CreateStoreSchema.parse({ name: '  Rite Aid  ' });
    expect(result.name).toBe('Rite Aid');
  });
});

describe('UpdateStoreSchema', () => {
  it('accepts empty object', () => {
    expect(UpdateStoreSchema.safeParse({}).success).toBe(true);
  });

  it('accepts partial update', () => {
    const result = UpdateStoreSchema.safeParse({ isActive: false });
    expect(result.success).toBe(true);
  });

  it('accepts tags update', () => {
    const result = UpdateStoreSchema.safeParse({ tags: ['grocery'] });
    expect(result.success).toBe(true);
  });
});

describe('StoreQuerySchema', () => {
  it('applies default limit', () => {
    const result = StoreQuerySchema.parse({});
    expect(result.limit).toBe(20);
  });

  it('coerces limit from string', () => {
    const result = StoreQuerySchema.parse({ limit: '5' });
    expect(result.limit).toBe(5);
  });

  it('accepts tags and search filters', () => {
    const result = StoreQuerySchema.parse({ tags: 'pharmacy', search: 'walgreens' });
    expect(result.tags).toBe('pharmacy');
    expect(result.search).toBe('walgreens');
  });
});
