import type { IPropertyProvider, PropertyFilters } from './IPropertyProvider'
import type { Property, CreatePropertyInput, UpdatePropertyInput } from '../domain/property'
import { mockProperties } from '../mock-data/properties'

export const propertyStore: Property[] = [...mockProperties]
const store = propertyStore

export const MockupPropertyProvider: IPropertyProvider = {
  async getAll(filters?: PropertyFilters) {
    let results = [...store]
    if (filters?.assetType) results = results.filter(p => p.assetType === filters.assetType)
    if (filters?.resultType) results = results.filter(p => p.resultType === filters.resultType)
    if (filters?.city) results = results.filter(p => p.location.city.toLowerCase().includes(filters.city!.toLowerCase()))
    if (filters?.minAreaSqm) results = results.filter(p => p.areaSqm >= filters.minAreaSqm!)
    if (filters?.maxRentPerSqm) results = results.filter(p => p.rentPricePerSqm <= filters.maxRentPerSqm!)
    if (filters?.organizationId) results = results.filter(p => p.organizationId === filters.organizationId)
    return results
  },
  async getById(id) {
    return store.find(p => p.id === id) ?? null
  },
  async create(data: CreatePropertyInput) {
    const next: Property = { id: crypto.randomUUID(), ...data, createdAt: new Date().toISOString(), updatedAt: new Date().toISOString() }
    store.push(next)
    return next
  },
  async update(id, data: UpdatePropertyInput) {
    const idx = store.findIndex(p => p.id === id)
    store[idx] = { ...store[idx], ...data, updatedAt: new Date().toISOString() }
    return store[idx]
  },
  async remove(id) {
    const idx = store.findIndex(p => p.id === id)
    store.splice(idx, 1)
  },
}
