import { Client, Account, Databases, Storage, ID, Query } from 'appwrite'

/** Ticket-System Appwrite-Projekt (Fallback wenn keine .env beim Build) */
const DEFAULT_PROJECT_ID = '6a1058610003c5a13a05'

const endpoint = import.meta.env.VITE_APPWRITE_ENDPOINT || 'https://ticket.webklar.com/v1'
export const projectId = (
  import.meta.env.VITE_APPWRITE_PROJECT_ID || DEFAULT_PROJECT_ID
).trim()

/** Demo-Modus nur wenn wirklich keine Projekt-ID (sollte mit Defaults nie passieren) */
export const isDemoMode = !projectId

if (import.meta.env.DEV) {
  console.log('🔧 Appwrite Konfiguration:')
  console.log('Endpoint:', endpoint)
  console.log('Project ID:', projectId || 'NICHT GESETZT')
  console.log('Demo-Modus:', isDemoMode)
  console.log('Database ID:', import.meta.env.VITE_APPWRITE_DATABASE_ID || 'woms-database')
}

if (!projectId) {
  console.error('❌ FEHLER: VITE_APPWRITE_PROJECT_ID ist nicht gesetzt!')
}

const client = new Client().setEndpoint(endpoint).setProject(projectId)

export const account = new Account(client)
export const databases = new Databases(client)
export const storage = new Storage(client)

export const DATABASE_ID = import.meta.env.VITE_APPWRITE_DATABASE_ID || 'woms-database'

export const COLLECTIONS = {
  WORKORDERS: 'workorders',
  CONFIG: 'config',
  CUSTOMERS: 'customers',
  EMPLOYEES: 'employees',
  WORKSHEETS: 'worksheets',
  USERS: 'users',
  ATTACHMENTS: 'attachments',
  WEBSITE_PROJECTS: 'websiteProjects',
  LEADS: 'leads',
  LEAD_SETTINGS: 'leadSettings',
  LEAD_PIPELINE: 'leadPipeline',
  ROADMAPS: 'roadmaps',
  ROADMAP_NODES: 'roadmapNodes',
}

export const WEBPAGE_TICKET_TYPE = 'Webpage'
export const PROJECT_TICKET_TYPE = 'Project'
export const PREVIEW_TEMPLATE_NAME = 'webklar-preview-template'

export const BUCKET_ID = import.meta.env.VITE_APPWRITE_BUCKET_ID || 'woms-attachments'

/** Prüft ob vermutlich eine Session existiert (cookieFallback oder Same-Origin-Cookie). */
export function hasAppwriteSession() {
  if (typeof window === 'undefined' || !projectId) return false
  try {
    const apiHost = new URL(endpoint).hostname
    if (apiHost === window.location.hostname) {
      return true
    }
  } catch {
    /* ignore */
  }
  try {
    const raw = window.localStorage.getItem('cookieFallback')
    if (!raw) return false
    const parsed = JSON.parse(raw)
    return Boolean(parsed?.[`a_session_${projectId}`])
  } catch {
    return false
  }
}

export { ID, Query }
