import { Note } from "@/types"
import { leads } from "./leads"
import { users } from "./users"

const noteTemplates = [
  "Called the prospect to discuss their requirements. They are looking for a complete website overhaul with e-commerce capabilities.",
  "Sent over the proposal and pricing breakdown. Waiting for their feedback on the package options.",
  "Followed up via email. They mentioned they're comparing a few agencies but liked our portfolio.",
  "Had a discovery call. They need the project completed within 8 weeks. Budget seems flexible.",
  "Sent a customized demo of our recent e-commerce work. They were particularly impressed with the checkout flow.",
  "Received their requirements document. Scope includes 12 pages, blog integration, and a contact form.",
  "Meeting scheduled for next Tuesday to discuss technical specifications in detail.",
  "They requested references from similar projects. Sent over three case studies.",
  "Discussed hosting options and ongoing maintenance packages. They're interested in a monthly retainer.",
  "Sent over the contract for signature. Waiting for their legal team to review.",
  "They asked about our experience with custom CRM integrations. Shared relevant project examples.",
  "Follow-up call completed. They have some additional questions about the timeline.",
  "Exchanged emails about the design direction. They prefer a minimalist approach.",
  "Discussed SEO strategy and content creation. They want to handle content themselves.",
  "They requested a revised quote for a smaller scope. Adjusted the proposal accordingly.",
  "Had a productive call about their target audience and user personas.",
  "Sent over wireframes for the homepage. Waiting for their feedback.",
  "They mentioned a referral from one of our past clients - good rapport building.",
  "Discussed payment terms. They prefer milestone-based payments.",
  "They're ready to move forward! Sending the final contract today.",
  "Checked in to see if they had any questions about the proposal. No response yet.",
  "Left a voicemail. Will try again next week.",
  "They mentioned their budget is slightly lower than our minimum. Discussed scope adjustments.",
  "Great meeting! They love our approach and want to start as soon as possible.",
  "They need to get approval from their partner before proceeding.",
]

function randomDateNear(leadDate: string, maxDaysAfter: number): string {
  const base = new Date(leadDate)
  const offset = Math.floor(Math.random() * maxDaysAfter * 24 * 60 * 60 * 1000)
  return new Date(base.getTime() + offset).toISOString()
}

export const notes: Note[] = leads.flatMap((lead) => {
  const leadNotes: Note[] = []
  const numNotes = Math.floor(Math.random() * 4) + 1

  const assignedUser = lead.assignedUser ?? users[0]

  for (let i = 0; i < numNotes; i++) {
    const author = Math.random() > 0.3 ? assignedUser : users[Math.floor(Math.random() * users.length)]
    const createdAt = randomDateNear(lead.createdAt, 45)
    leadNotes.push({
      id: `note-${lead.id}-${i}`,
      leadId: lead.id,
      userId: author.id,
      authorName: author.name,
      authorAvatar: author.avatar,
      authorRole: author.role,
      note: noteTemplates[Math.floor(Math.random() * noteTemplates.length)],
      createdAt,
      updatedAt: createdAt,
    })
  }

  return leadNotes
}).sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime())

export function getNotesByLeadId(leadId: string): Note[] {
  return notes.filter((n) => n.leadId === leadId).sort(
    (a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime()
  )
}
