import {
  TResponseData,
  TResponseTtc,
  TResponseUpdate,
  TResponseVariables,
} from "@formbricks/types/responses";
import { type TWebSurveyMeta } from "./browser-context";
import { type TNotShownStep } from "./not-shown-record";

const DB_NAME = "formbricks-offline";
const DB_VERSION = 1;

const STORE_PENDING_RESPONSES = "pendingResponses";
const STORE_SURVEY_PROGRESS = "surveyProgress";

export interface SerializedSurveyState {
  responseId: string | null;
  displayId: string | null;
  surveyId: string;
  singleUseId: string | null;
  userId: string | null;
  contactId: string | null;
  responseAcc: TResponseUpdate;
}

export interface PendingResponseEntry {
  id?: number;
  surveyId: string;
  responseUpdate: TResponseUpdate;
  surveyStateSnapshot: SerializedSurveyState;
  createdAt: number;
}

export interface SurveyProgressEntry {
  surveyId: string;
  blockId: string;
  responseData: TResponseData;
  ttc: TResponseTtc;
  currentVariables: TResponseVariables;
  history: string[];
  /**
   * The not-shown steps next to `history` (ENG-6936), so a resumed session keeps sending the whole
   * record. Optional: entries written before it restore with none, as the bundle that wrote them sent.
   */
  notShownSteps?: TNotShownStep[];
  /**
   * A write already carried the Low quality flag (ENG-6933, P42), so a resumed session doesn't send
   * it again. Optional: entries written before it restore without it.
   */
  lowQualitySent?: boolean;
  selectedLanguage: string;
  surveyStateSnapshot: SerializedSurveyState;
  /**
   * The frozen browser-runtime context (ENG-1841), persisted for the same reason `responseId` is: a
   * resumed offline session keeps writing to the response it already created, so the meta it reports
   * must keep describing the display that created it. Without this, a reload rebuilds the snapshot
   * from the *new* page — a different URL, referrer or viewport — and the second write overwrites the
   * first one's answer to "where was this survey shown".
   *
   * Optional because entries written before this field existed must still restore; those resume with
   * the freshly measured snapshot, which is the behaviour they already had.
   */
  webSurveyMeta?: TWebSurveyMeta;
  updatedAt: number;
}

let dbInstance: IDBDatabase | null = null;
let dbPromise: Promise<IDBDatabase> | null = null;

const isIndexedDBAvailable = (): boolean => {
  try {
    return typeof indexedDB !== "undefined" && indexedDB !== null;
  } catch {
    return false;
  }
};

const openDb = (): Promise<IDBDatabase> => {
  if (dbInstance) return Promise.resolve(dbInstance);
  if (dbPromise) return dbPromise;

  dbPromise = new Promise((resolve, reject) => {
    if (!isIndexedDBAvailable()) {
      reject(new Error("IndexedDB is not available"));
      return;
    }

    const request = indexedDB.open(DB_NAME, DB_VERSION);

    request.onupgradeneeded = () => {
      const db = request.result;

      if (!db.objectStoreNames.contains(STORE_PENDING_RESPONSES)) {
        const store = db.createObjectStore(STORE_PENDING_RESPONSES, {
          keyPath: "id",
          autoIncrement: true,
        });
        store.createIndex("surveyId", "surveyId", { unique: false });
      }

      if (!db.objectStoreNames.contains(STORE_SURVEY_PROGRESS)) {
        db.createObjectStore(STORE_SURVEY_PROGRESS, { keyPath: "surveyId" });
      }
    };

    request.onsuccess = () => {
      dbInstance = request.result;
      dbPromise = null;

      dbInstance.onclose = () => {
        dbInstance = null;
      };

      dbInstance.onerror = () => {
        dbInstance = null;
      };

      dbInstance.onversionchange = () => {
        dbInstance?.close();
        dbInstance = null;
      };

      resolve(dbInstance);
    };

    request.onblocked = () => {
      console.warn("Formbricks: IndexedDB open blocked by another connection");
    };

    request.onerror = () => {
      dbPromise = null;
      reject(request.error ?? new Error("IndexedDB open failed"));
    };
  });

  return dbPromise;
};

export const addPendingResponse = async (entry: Omit<PendingResponseEntry, "id">): Promise<number> => {
  try {
    const db = await openDb();
    return new Promise((resolve, reject) => {
      const tx = db.transaction(STORE_PENDING_RESPONSES, "readwrite");
      const store = tx.objectStore(STORE_PENDING_RESPONSES);
      const request = store.add(entry);

      request.onsuccess = () => resolve(request.result as number);
      request.onerror = () => reject(request.error ?? new Error("IndexedDB request failed"));
    });
  } catch (e) {
    console.warn("Formbricks: Failed to persist response to IndexedDB", e);
    return -1;
  }
};

export const getPendingResponsesStrict = async (surveyId: string): Promise<PendingResponseEntry[]> => {
  const db = await openDb();
  return new Promise((resolve, reject) => {
    const tx = db.transaction(STORE_PENDING_RESPONSES, "readonly");
    const store = tx.objectStore(STORE_PENDING_RESPONSES);
    const index = store.index("surveyId");
    const request = index.getAll(surveyId);

    request.onsuccess = () => {
      const results = (request.result as PendingResponseEntry[]).sort((a, b) => a.createdAt - b.createdAt);
      resolve(results);
    };
    request.onerror = () => reject(request.error ?? new Error("IndexedDB request failed"));
  });
};

export const getPendingResponses = async (surveyId: string): Promise<PendingResponseEntry[]> => {
  try {
    return await getPendingResponsesStrict(surveyId);
  } catch (e) {
    console.warn("Formbricks: Failed to read pending responses from IndexedDB", e);
    return [];
  }
};

export const removePendingResponseStrict = async (id: number): Promise<void> => {
  const db = await openDb();
  return new Promise((resolve, reject) => {
    const tx = db.transaction(STORE_PENDING_RESPONSES, "readwrite");
    const store = tx.objectStore(STORE_PENDING_RESPONSES);
    const request = store.delete(id);

    request.onsuccess = () => resolve();
    request.onerror = () => reject(request.error ?? new Error("IndexedDB request failed"));
  });
};

export const removePendingResponse = async (id: number): Promise<void> => {
  try {
    return await removePendingResponseStrict(id);
  } catch (e) {
    console.warn("Formbricks: Failed to remove pending response from IndexedDB", e);
  }
};

export const countPendingResponsesStrict = async (surveyId: string): Promise<number> => {
  const db = await openDb();
  return new Promise((resolve, reject) => {
    const tx = db.transaction(STORE_PENDING_RESPONSES, "readonly");
    const index = tx.objectStore(STORE_PENDING_RESPONSES).index("surveyId");
    const request = index.count(IDBKeyRange.only(surveyId));

    request.onsuccess = () => resolve(request.result);
    request.onerror = () => reject(request.error ?? new Error("IndexedDB request failed"));
  });
};

export const countPendingResponses = async (surveyId: string): Promise<number> => {
  try {
    return await countPendingResponsesStrict(surveyId);
  } catch (e) {
    console.warn("Formbricks: Failed to count pending responses from IndexedDB", e);
    return 0;
  }
};

export const clearPendingResponses = async (surveyId: string): Promise<void> => {
  try {
    const db = await openDb();
    const tx = db.transaction(STORE_PENDING_RESPONSES, "readwrite");
    const store = tx.objectStore(STORE_PENDING_RESPONSES);
    const index = store.index("surveyId");
    const cursorRequest = index.openCursor(IDBKeyRange.only(surveyId));

    return new Promise((resolve, reject) => {
      cursorRequest.onsuccess = () => {
        const cursor = cursorRequest.result;
        if (cursor) {
          cursor.delete();
          cursor.continue();
        }
      };
      tx.oncomplete = () => resolve();
      tx.onerror = () => reject(tx.error ?? new Error("IndexedDB transaction failed"));
    });
  } catch (e) {
    console.warn("Formbricks: Failed to clear pending responses from IndexedDB", e);
  }
};

export const saveSurveyProgress = async (progress: SurveyProgressEntry): Promise<void> => {
  try {
    const db = await openDb();
    return new Promise((resolve, reject) => {
      const tx = db.transaction(STORE_SURVEY_PROGRESS, "readwrite");
      const store = tx.objectStore(STORE_SURVEY_PROGRESS);
      const request = store.put(progress);

      request.onsuccess = () => resolve();
      request.onerror = () => reject(request.error ?? new Error("IndexedDB request failed"));
    });
  } catch (e) {
    console.warn("Formbricks: Failed to save survey progress to IndexedDB", e);
  }
};

export const getSurveyProgress = async (surveyId: string): Promise<SurveyProgressEntry | undefined> => {
  try {
    const db = await openDb();
    return new Promise((resolve, reject) => {
      const tx = db.transaction(STORE_SURVEY_PROGRESS, "readonly");
      const store = tx.objectStore(STORE_SURVEY_PROGRESS);
      const request = store.get(surveyId);

      request.onsuccess = () => resolve(request.result as SurveyProgressEntry | undefined);
      request.onerror = () => reject(request.error ?? new Error("IndexedDB request failed"));
    });
  } catch (e) {
    console.warn("Formbricks: Failed to read survey progress from IndexedDB", e);
    return undefined;
  }
};

export const patchSurveyProgressSnapshot = async (
  surveyId: string,
  snapshotPatch: Partial<SerializedSurveyState>
): Promise<void> => {
  try {
    const db = await openDb();
    return new Promise((resolve, reject) => {
      const tx = db.transaction(STORE_SURVEY_PROGRESS, "readwrite");
      const store = tx.objectStore(STORE_SURVEY_PROGRESS);
      const getRequest = store.get(surveyId);

      getRequest.onsuccess = () => {
        const existing = getRequest.result as SurveyProgressEntry | undefined;
        if (!existing) {
          resolve();
          return;
        }

        const putRequest = store.put({
          ...existing,
          surveyStateSnapshot: {
            ...existing.surveyStateSnapshot,
            ...snapshotPatch,
          },
          updatedAt: Date.now(),
        });

        putRequest.onsuccess = () => resolve();
        putRequest.onerror = () => reject(putRequest.error ?? new Error("IndexedDB request failed"));
      };

      getRequest.onerror = () => reject(getRequest.error ?? new Error("IndexedDB request failed"));
    });
  } catch (e) {
    console.warn("Formbricks: Failed to patch survey progress snapshot in IndexedDB", e);
  }
};

export const clearSurveyProgress = async (surveyId: string): Promise<void> => {
  try {
    const db = await openDb();
    return new Promise((resolve, reject) => {
      const tx = db.transaction(STORE_SURVEY_PROGRESS, "readwrite");
      const store = tx.objectStore(STORE_SURVEY_PROGRESS);
      const request = store.delete(surveyId);

      request.onsuccess = () => resolve();
      request.onerror = () => reject(request.error ?? new Error("IndexedDB request failed"));
    });
  } catch (e) {
    console.warn("Formbricks: Failed to clear survey progress from IndexedDB", e);
  }
};
