// mock/db.js — simulates the full backend API
// Swap each function for a real fetch() call when wiring the real backend

const mockUsers = {
  user_maya: {
    id: 'user_maya',
    email: 'maya@studio.com',
    name: 'Maya',
    credits_balance: 4,
    free_used: true,
  },
  user_richard: {
    id: 'user_richard',
    email: 'richard@firmname.com',
    name: 'Richard',
    credits_balance: 0,
    free_used: true,
  },
  user_new: {
    id: 'user_new',
    email: 'new@user.com',
    name: 'New User',
    credits_balance: 0,
    free_used: false,
  },
}

let activeUserId = null

const mockScanResponse = {
  session_id: 'scan_abc123',
  pages: 12,
  stories: 8,
  images: 14,
  tables: 3,
  fonts: [
    { name: 'Arial',            status: 'safe',         substitute: null,      substitute_quality: null,                  used_for: 'Headings' },
    { name: 'Freight Text Pro', status: 'professional', substitute: 'Georgia', substitute_quality: 'serif body substitute', used_for: 'Body text (pages 1–8)' },
    { name: 'Myriad Pro',       status: 'professional', substitute: 'Calibri', substitute_quality: 'sans-serif substitute', used_for: 'Captions (pages 3–12)' },
    { name: 'DM Mono',          status: 'unknown',      substitute: null,      substitute_quality: null,                  used_for: 'Code blocks (page 7)' },
  ],
  warnings: [
    { type: 'wrap_simplified', severity: 'info', page: 4,    message: 'Contour text wrap simplified to square wrap' },
    { type: 'wrap_simplified', severity: 'info', page: 7,    message: 'Contour text wrap simplified to square wrap' },
    { type: 'master_page',     severity: 'info', page: null, message: 'Master page elements excluded — page numbers handled separately' },
  ],
}

const delay = (ms) => new Promise((r) => setTimeout(r, ms))

export const mockApi = {
  // POST /api/scan
  async scan(file) {
    await delay(1800)
    if (file && !file.name.endsWith('.idml') && !file.name.endsWith('.zip')) {
      throw new Error('This is not a valid IDML file. Please upload an IDML export from InDesign.')
    }
    return { ...mockScanResponse }
  },

  // POST /api/convert
  async convert(sessionId) {
    await delay(3200)
    const user = activeUserId ? mockUsers[activeUserId] : null
    if (!user) throw new Error('Not authenticated')
    if (user.free_used && user.credits_balance < 1) {
      throw new Error('INSUFFICIENT_CREDITS')
    }
    if (user.free_used) {
      mockUsers[activeUserId].credits_balance -= 1
    } else {
      mockUsers[activeUserId].free_used = true
    }
    return { download_url: '#mock-download', filename: 'AnnualReport_2025.docx' }
  },

  // GET /api/users/me
  async getMe() {
    await delay(200)
    if (!activeUserId) return null
    return { ...mockUsers[activeUserId] }
  },

  // POST /api/auth/login
  async login(email, password) {
    await delay(600)
    const user = Object.values(mockUsers).find((u) => u.email === email)
    if (!user) throw new Error('Invalid credentials')
    activeUserId = user.id
    return { ...user, token: 'mock_token_' + user.id }
  },

  // POST /api/auth/logout
  async logout() {
    await delay(200)
    activeUserId = null
  },

  // POST /api/auth/register
  async register(email, password, name) {
    await delay(800)
    const newId = 'user_' + Date.now()
    mockUsers[newId] = { id: newId, email, name, credits_balance: 0, free_used: false }
    activeUserId = newId
    return { ...mockUsers[newId], token: 'mock_token_' + newId }
  },

  // POST /payments/create-intent
  async createPaymentIntent(pack) {
    await delay(500)
    const packs = {
      starter: { credits: 5,  amount: 1900 },
      studio:  { credits: 20, amount: 5900 },
      agency:  { credits: 60, amount: 14900 },
    }
    return { client_secret: 'mock_pi_secret', ...packs[pack] }
  },

  // Simulate successful payment (skips Stripe in mock)
  async completePurchase(pack) {
    await delay(1000)
    const credits = { starter: 5, studio: 20, agency: 60 }
    if (activeUserId) {
      mockUsers[activeUserId].credits_balance += credits[pack]
    }
    return {
      success: true,
      new_balance: activeUserId ? mockUsers[activeUserId].credits_balance : 0,
    }
  },
}
