import { defineStore } from 'pinia';
import { ref, computed } from 'vue';

interface SearchFilters {
  minPrice?: number;
  maxPrice?: number;
  sortBy?: string;
  roomType?: string;
}

interface SearchHistory {
  keyword: string;
  time: number;
}

export const useSearchStore = defineStore('search', () => {
  // 城市选择
  const selectedCity = ref(uni.getStorageSync('selectedCity') || '上海');

  // 日期选择
  const today = new Date();
  const tomorrow = new Date(today);
  tomorrow.setDate(tomorrow.getDate() + 1);

  const checkInDate = ref(uni.getStorageSync('checkInDate') || formatDate(today));
  const checkOutDate = ref(uni.getStorageSync('checkOutDate') || formatDate(tomorrow));

  // 计算入住天数
  const nightCount = computed(() => {
    const checkIn = new Date(checkInDate.value);
    const checkOut = new Date(checkOutDate.value);
    const diff = checkOut.getTime() - checkIn.getTime();
    return Math.max(1, Math.ceil(diff / (1000 * 60 * 60 * 24)));
  });

  // 筛选条件
  const filters = ref<SearchFilters>({
    minPrice: undefined,
    maxPrice: undefined,
    sortBy: undefined,
    roomType: undefined,
  });

  // 搜索历史
  const searchHistory = ref<SearchHistory[]>(
    uni.getStorageSync('searchHistory') || []
  );

  // 设置城市
  function setCity(city: string) {
    selectedCity.value = city;
    uni.setStorageSync('selectedCity', city);
  }

  // 设置日期
  function setDates(checkIn: string, checkOut: string) {
    checkInDate.value = checkIn;
    checkOutDate.value = checkOut;
    uni.setStorageSync('checkInDate', checkIn);
    uni.setStorageSync('checkOutDate', checkOut);
  }

  // 设置筛选条件
  function setFilters(newFilters: SearchFilters) {
    filters.value = { ...filters.value, ...newFilters };
  }

  // 重置筛选条件
  function resetFilters() {
    filters.value = {
      minPrice: undefined,
      maxPrice: undefined,
      sortBy: undefined,
      roomType: undefined,
    };
  }

  // 添加搜索历史
  function addSearchHistory(keyword: string) {
    const newHistory: SearchHistory = {
      keyword,
      time: Date.now(),
    };
    // 去重
    const existing = searchHistory.value.findIndex((h) => h.keyword === keyword);
    if (existing > -1) {
      searchHistory.value.splice(existing, 1);
    }
    searchHistory.value.unshift(newHistory);
    // 最多保留10条
    if (searchHistory.value.length > 10) {
      searchHistory.value = searchHistory.value.slice(0, 10);
    }
    uni.setStorageSync('searchHistory', searchHistory.value);
  }

  // 清空搜索历史
  function clearSearchHistory() {
    searchHistory.value = [];
    uni.removeStorageSync('searchHistory');
  }

  return {
    selectedCity,
    checkInDate,
    checkOutDate,
    nightCount,
    filters,
    searchHistory,
    setCity,
    setDates,
    setFilters,
    resetFilters,
    addSearchHistory,
    clearSearchHistory,
  };
});

// 格式化日期为 YYYY-MM-DD
function formatDate(date: Date): string {
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0');
  const day = String(date.getDate()).padStart(2, '0');
  return `${year}-${month}-${day}`;
}
