import { defineStore } from "pinia";

export const useWebsiteStore = defineStore("website", {
  state: () => ({
    data: null as any | null,
    websiteId: 5, // ✅ fixed default — never changes based on company
    loaded: false,
  }),
  getters: {
    getData: (state) => state.data,
  },
  actions: {
    setWebsiteId(id: number) {
      this.websiteId = id;
    },
    async load(id: number | null = null) {
      // ✅ Always use websiteId (2) — ignore any passed company id
      const targetId = this.websiteId;
      try {
        const res = await ifetch("website/website_get_by_id", { id: targetId });
        this.data = res ?? null;
        this.loaded = true;
        return this.data;
      } catch (err) {
        console.error("Failed to load website data:", err);
        this.data = null;
        this.loaded = false;
        return null;
      }
    },
  },
});