import { defineStore } from 'pinia';
import { linksApi } from '../api/links.js';
import { useWorkspaceStore } from './workspace.js';

export const useLinksStore = defineStore('links', {
  state: () => ({
    links: [],
    loading: false,
    error: null,
  }),

  actions: {
    async fetchLinks(params = {}) {
      const workspaceStore = useWorkspaceStore();
      if (!workspaceStore.currentWorkspaceId) return;

      this.loading = true;
      try {
        const response = await linksApi.list(workspaceStore.currentWorkspaceId, params);
        this.links = response.links || [];
      } catch (err) {
        this.error = err.message;
      } finally {
        this.loading = false;
      }
    },

    async createLink(data) {
      const workspaceStore = useWorkspaceStore();
      if (!workspaceStore.currentWorkspaceId) return;

      try {
        const link = await linksApi.create(workspaceStore.currentWorkspaceId, data);
        this.links.unshift(link);
        return link;
      } catch (err) {
        this.error = err.message;
        throw err;
      }
    },

    async updateLink(id, data) {
      const workspaceStore = useWorkspaceStore();
      if (!workspaceStore.currentWorkspaceId) return;

      try {
        const link = await linksApi.update(workspaceStore.currentWorkspaceId, id, data);
        const index = this.links.findIndex(l => l.id === id);
        if (index !== -1) {
          this.links[index] = link;
        }
        return link;
      } catch (err) {
        this.error = err.message;
        throw err;
      }
    },

    async deleteLink(id) {
      const workspaceStore = useWorkspaceStore();
      if (!workspaceStore.currentWorkspaceId) return;

      try {
        await linksApi.delete(workspaceStore.currentWorkspaceId, id);
        this.links = this.links.filter(l => l.id !== id);
      } catch (err) {
        this.error = err.message;
        throw err;
      }
    },

    async restoreLink(id) {
      const workspaceStore = useWorkspaceStore();
      if (!workspaceStore.currentWorkspaceId) return;

      try {
        const link = await linksApi.restore(workspaceStore.currentWorkspaceId, id);
        const index = this.links.findIndex(l => l.id === id);
        if (index !== -1) {
          this.links[index] = link;
        }
        return link;
      } catch (err) {
        this.error = err.message;
        throw err;
      }
    },

    async bulkCreateLinks(links) {
      const workspaceStore = useWorkspaceStore();
      if (!workspaceStore.currentWorkspaceId) return;

      try {
        const result = await linksApi.bulkCreate(workspaceStore.currentWorkspaceId, links);
        await this.fetchLinks();
        return result;
      } catch (err) {
        this.error = err.message;
        throw err;
      }
    },

    async getLinkAnalytics(linkId, period = '7d', startDate = null, endDate = null) {
      const workspaceStore = useWorkspaceStore();
      if (!workspaceStore.currentWorkspaceId) return;

      try {
        return await linksApi.getAnalytics(workspaceStore.currentWorkspaceId, linkId, period, startDate, endDate);
      } catch (err) {
        this.error = err.message;
        throw err;
      }
    },

    clearAll() {
      this.links = [];
    },
  },
});
