import { Ref, ref } from 'vue';
import { IntervalTemplate } from './schedule.types';
import { defineStore } from 'pinia';
import { AppwriteIds, databases } from 'src/boot/appwrite';
import { ID, Models } from 'appwrite';
import { arrayToTimeTuples } from 'src/utils/schedule';

export const useIntervalTemplateStore = defineStore('intervalTemplate', () => {
  const intervalTemplates = ref<IntervalTemplate[]>([]);

  const getIntervalTemplates = (): Ref<IntervalTemplate[]> => {
    // Should subscribe to get new intervaltemplates when they are created
    if (!intervalTemplates.value) fetchIntervalTemplates();
    return intervalTemplates;
  };

  async function fetchIntervalTemplates() {
    try {
      const response = await databases.listDocuments(
        AppwriteIds.databaseId,
        AppwriteIds.collection.intervalTemplate
      );
      intervalTemplates.value = response.documents.map(
        (d: Models.Document): IntervalTemplate => {
          return {
            ...d,
            timeTuples: arrayToTimeTuples(d.timeTuple),
          } as IntervalTemplate;
        }
      );
    } catch (error) {
      console.error('Failed to fetch timeblock templates', error);
    }
  }

  const createIntervalTemplate = async (template: IntervalTemplate) => {
    try {
      const response = await databases.createDocument(
        AppwriteIds.databaseId,
        AppwriteIds.collection.intervalTemplate,
        ID.unique(),
        { name: template.name, timeTuple: template.timeTuples.flat(2) }
      );
      intervalTemplates.value.push(response as IntervalTemplate);
    } catch (e) {
      console.error('Error updating IntervalTemplate: ' + e);
    }
  };
  const deleteIntervalTemplate = async (id: string) => {
    try {
      await databases.deleteDocument(
        AppwriteIds.databaseId,
        AppwriteIds.collection.intervalTemplate,
        id
      );
      intervalTemplates.value = intervalTemplates.value.filter(
        (template) => template.$id !== id
      );
    } catch (e) {
      console.error('Error deleting IntervalTemplate: ' + e);
    }
  };
  const updateIntervalTemplate = async (
    template: IntervalTemplate,
    id: string
  ) => {
    try {
      const response = await databases.updateDocument(
        AppwriteIds.databaseId,
        AppwriteIds.collection.intervalTemplate,
        id,
        {
          name: template.name,
          timeTuple: template.timeTuples.flat(2),
        }
      );
      intervalTemplates.value = intervalTemplates.value.map((b) =>
        b.$id !== id
          ? b
          : ({
              ...response,
              timeTuples: arrayToTimeTuples(response.timeTuple),
            } as IntervalTemplate)
      );
    } catch (e) {
      console.error('Error updating IntervalTemplate: ' + e);
    }
  };

  return {
    getIntervalTemplates,
    fetchIntervalTemplates,
    createIntervalTemplate,
    deleteIntervalTemplate,
    updateIntervalTemplate,
  };
});
