'use client';

/**
 * Zustand logo store — persisted to localStorage.
 *
 * Stores the customer logo URL (from S3) and its S3 object key.
 * Only Super_Admin users can upload/remove the logo via the Settings page,
 * but all roles see it once set.
 */

import { create } from 'zustand';
import { persist } from 'zustand/middleware';

export interface LogoState {
  /** Public URL of the uploaded logo on S3, or null if none */
  logoUrl: string | null;
  /** S3 object key, needed for deletion */
  logoKey: string | null;
  /** Logo height in the header (px) */
  logoHeight: number;
  /** Whether an upload/delete is in progress */
  isUploading: boolean;
  setLogo: (url: string, key: string) => void;
  removeLogo: () => void;
  setUploading: (v: boolean) => void;
  setLogoHeight: (h: number) => void;
}

export const useLogoStore = create<LogoState>()(
  persist(
    (set) => ({
      logoUrl: null,
      logoKey: null,
      logoHeight: 32,
      isUploading: false,

      setLogo: (url: string, key: string) => {
        set({ logoUrl: url, logoKey: key, isUploading: false });
      },

      removeLogo: () => {
        set({ logoUrl: null, logoKey: null, isUploading: false });
      },

      setUploading: (v: boolean) => {
        set({ isUploading: v });
      },

      setLogoHeight: (h: number) => {
        set({ logoHeight: h });
      },
    }),
    {
      name: 'divisi-customer-logo',
      partialize: (state) => ({
        logoUrl: state.logoUrl,
        logoKey: state.logoKey,
        logoHeight: state.logoHeight,
      }),
    },
  ),
);
