import mock from './mock'
import type { Course } from './types'
import avatar1 from '@/assets/avatars/avatar-1.png'
import appDevelopment from '@/assets/pages/app-development.jpg'
import dataAnalysis from '@/assets/pages/data-analysis.jpg'
import dataPrivacy from '@/assets/pages/data-privacy.jpg'
import designCourse from '@/assets/pages/design-course.jpg'
import hacking from '@/assets/pages/ethical-hacking-course.jpg'
import graphicBasic from '@/assets/pages/graphic-basic.jpg'
import javascriptMastery from '@/assets/pages/javascript-mastery.jpg'
import machineLearning from '@/assets/pages/machine-learning.jpg'
import marketingStrategy from '@/assets/pages/marketing-strategy.jpg'
import pythonProgramming from '@/assets/pages/python-programming.jpg'

const courses: Course[] = [
  {
    name: 'Design Mastery',
    description: 'UI/UX design mastery',
    chapter: 20,
    hours: 34,
    tutor: { name: 'Oozy Osborne', img: avatar1 },
    rating: 4.5,
    enrolls: 10000,
    price: 90,
    discount: 10,
    purchased: true,
    courseBanner: designCourse,
    lastUpdated: '02/25/2024',
    releaseDate: '02/25/2024',
  },
  {
    name: 'Web Development Fundamentals',
    description: 'Learn the basics of web development',
    chapter: 15,
    hours: 25,
    tutor: { name: 'Cody Coder', img: avatar1 },
    rating: 4.2,
    enrolls: 8500,
    price: 75,
    discount: 15,
    purchased: false,
    courseBanner: hacking,
    lastUpdated: '02/25/2023',
    releaseDate: '02/25/2023',
  },
  {
    name: 'Data Science Essentials',
    description: 'Introduction to data science concepts',
    chapter: 18,
    hours: 30,
    tutor: { name: 'Data Wizard', img: avatar1 },
    rating: 4.7,
    enrolls: 12500,
    price: 0,
    discount: 0,
    purchased: false,
    courseBanner: dataAnalysis,
    lastUpdated: '02/25/2023',
    releaseDate: '02/25/2023',
  },
  {
    name: 'JavaScript Mastery',
    description: 'Mastering JavaScript programming',
    chapter: 25,
    hours: 40,
    tutor: { name: 'JS Guru', img: avatar1 },
    rating: 4.8,
    enrolls: 15000,
    price: 100,
    discount: 15,
    purchased: true,
    courseBanner: javascriptMastery,
    lastUpdated: '03/05/2024',
    releaseDate: '03/05/2024',
  },
  {
    name: 'Mobile App Development',
    description: 'Building mobile applications from scratch',
    chapter: 22,
    hours: 38,
    tutor: { name: 'App Developer', img: avatar1 },
    rating: 4.6,
    enrolls: 11000,
    price: 95,
    discount: 10,
    purchased: false,
    courseBanner: appDevelopment,
    lastUpdated: '03/10/2024',
    releaseDate: '02/25/2023',
  },
  {
    name: 'Graphic Design Basics',
    description: 'Fundamentals of graphic design',
    chapter: 12,
    hours: 20,
    tutor: { name: 'Design Pro', img: avatar1 },
    rating: 4.0,
    enrolls: 7000,
    price: 60,
    discount: 5,
    purchased: true,
    courseBanner: graphicBasic,
    lastUpdated: '02/25/2023',
    releaseDate: '02/25/2023',
  },
  {
    name: 'Cybersecurity Fundamentals',
    description: 'Essential concepts in cybersecurity',
    chapter: 18,
    hours: 32,
    tutor: { name: 'Cyber Defender', img: avatar1 },
    rating: 4.5,
    enrolls: 9000,
    price: 110,
    discount: 10,
    purchased: false,
    courseBanner: dataPrivacy,
    lastUpdated: '02/25/2023',
    releaseDate: '02/25/2023',
  },
  {
    name: 'Machine Learning Basics',
    description: 'Introduction to machine learning',
    chapter: 20,
    hours: 36,
    tutor: { name: 'ML Enthusiast', img: avatar1 },
    rating: 4.4,
    enrolls: 100,
    price: 85,
    discount: 12,
    purchased: true,
    courseBanner: machineLearning,
    lastUpdated: '02/25/2023',
    releaseDate: '02/25/2023',
  },
  {
    name: 'Digital Marketing Strategies',
    description: 'Effective digital marketing techniques',
    chapter: 15,
    hours: 28,
    tutor: { name: 'Marketing Maven', img: avatar1 },
    rating: 4.3,
    enrolls: 8000,
    price: 70,
    discount: 8,
    purchased: false,
    courseBanner: marketingStrategy,
    lastUpdated: '03/01/2024',
    releaseDate: '02/25/2023',
  },
  {
    name: 'Python Programming Essentials',
    description: 'Fundamental concepts of Python programming',
    chapter: 16,
    hours: 24,
    tutor: { name: 'Pythonista', img: avatar1 },
    rating: 4.6,
    enrolls: 11500,
    price: 80,
    discount: 15,
    purchased: true,
    courseBanner: pythonProgramming,
    lastUpdated: '02/25/2023',
    releaseDate: '02/25/2023',
  },
]

mock.onGet('/academy/courses').reply(request => {
  const { q = '' } = request.params || { q: '' }

  const queryLower = q.toLowerCase()

  // if query my learning  return only purchased course
  if (queryLower === 'my-learning') {
    const filterDb = courses.filter(course => {
      return course.purchased
    })

    return [200, { courses: filterDb }]
  }

  // if query latest return only latest release course date should be withing one month of current date
  if (queryLower === 'latest') {
    const currentDate = new Date()
    const oneMonthAgo = new Date()

    oneMonthAgo.setMonth(oneMonthAgo.getMonth() - 1)

    const latestCourses = courses.filter(course => {
      const releaseDate = new Date(course.releaseDate)

      return releaseDate >= oneMonthAgo && releaseDate <= currentDate
    })

    return [200, { courses: latestCourses }]
  }

  // return recent updated course within one month
  if (queryLower === 'recent-updated') {
    const oneMonthAgo = new Date()

    oneMonthAgo.setMonth(oneMonthAgo.getMonth() - 1)

    const recentUpdatedCourses = courses.filter(course => {
      const releaseDate = new Date(course.releaseDate)
      const updatedDate = new Date(course.lastUpdated)

      return (
        updatedDate >= oneMonthAgo
        && releaseDate.getTime() !== updatedDate.getTime()
      )
    })

    return [200, { courses: recentUpdatedCourses }]
  }

  // filter buy free course
  if (queryLower === 'free') {
    const free = courses.filter(course => {
      return course.price === 0
    })

    return [200, { courses: free }]
  }

  return [200, { courses }]
})
