import { describe, expect, it } from 'vitest';

import type { ActualListing } from '../types';
import { filterListingsByMode, isNewBuildListing } from './listings';

function listing(listing_url: string): ActualListing {
  return {
    lat: 51.5,
    lon: -0.1,
    postcode: 'N1 1AA',
    listing_url,
    features: [],
  };
}

const newBuild = listing('https://www.rightmove.co.uk/properties/1#/?channel=RES_NEW');
const resale = listing('https://www.rightmove.co.uk/properties/2#/?channel=RES_BUY');
const otherPortal = listing('https://www.onthemarket.com/details/3/');

describe('isNewBuildListing', () => {
  it('flags RES_NEW channel URLs as new builds', () => {
    expect(isNewBuildListing(newBuild)).toBe(true);
  });

  it('treats RES_BUY (resale) as non-new', () => {
    expect(isNewBuildListing(resale)).toBe(false);
  });

  it('treats non-Rightmove portals as non-new', () => {
    expect(isNewBuildListing(otherPortal)).toBe(false);
  });
});

describe('filterListingsByMode', () => {
  const all = [newBuild, resale, otherPortal];

  it('returns everything for "all"', () => {
    expect(filterListingsByMode(all, 'all')).toEqual(all);
  });

  it('returns only new builds for "new"', () => {
    expect(filterListingsByMode(all, 'new')).toEqual([newBuild]);
  });

  it('excludes new builds for "non-new"', () => {
    expect(filterListingsByMode(all, 'non-new')).toEqual([resale, otherPortal]);
  });

  it('returns nothing for "none"', () => {
    expect(filterListingsByMode(all, 'none')).toEqual([]);
  });
});
