import axios from 'axios'
import { IMonthlyNews } from '../models/monthlyNews.model'
import { IPoll } from '../models/poll.model'
import { toast } from 'react-toastify'
export class MonthlyNewsService {

  /**
   * Creates a quotation and header for selected month
   * @param date
   * @param header
   * @param quote
   */
  public createMonthlyReport = async (
    date: Date,
    header: string,
    quote: string,
    token: string
  ): Promise<void> => {
    try {
      await axios.put(
        `/api/admin/monthlyNews`,
        {
          month: date.getMonth(),
          year: date.getFullYear(),
          header: header,
          quote: quote,
        },
        {
          headers: {
            'XSRF-TOKEN': token,
          },
        }
      )
      toast.success('Monthly news succesfully saved !')
    } catch (e) {
      toast.error('Failed to create monthly news')
      console.log(e)
    }
  }

  /**
   * Gets a quotation and header for selected month
   */
  public getSingleMonthlyReport = async (
    year: number,
    month: number,
    token: string
  ): Promise<IMonthlyNews | null> => {
    try {
      const { data } = await axios.get(
        `/api/admin/monthlyNews/${year}/${month}`,
        {
          headers: {
            'XSRF-TOKEN': token,
          },
        }
      )
      return data as IMonthlyNews
    } catch (e) {
      console.log('error', e)
      return null
    }
  }

  /**
   * Gets a poll with question and link for selected month
   */
  public getSinglePoll = async (
    year: number,
    month: number,
    token: string
  ): Promise<IPoll | null> => {
    try {
      const { data } = await axios.get(
        `/api/admin/poll/${year}/${month}`,
        {
          headers: {
            'XSRF-TOKEN': token,
          },
        }
      )
      return data as IPoll
    } catch (e) {
      console.log('error', e)
      return null
    }
  }

  /**
   * Creates a poll with question and link for selected month
   * @param date
   * @param question
   * @param link
   */
  public createPoll = async (
    date: Date,
    question: string,
    link: string,
    token: string
  ): Promise<void> => {
    try {
      await axios.put(
        `/api/admin/poll`,
        {
          month: date.getMonth(),
          year: date.getFullYear(),
          link: link,
          question: question,
        },
        {
          headers: {
            'XSRF-TOKEN': token,
          },
        }
      )
      toast.success('Poll successfully saved !')
    } catch (e) {
      toast.error('Failed to create poll')
      console.log(e)
    }
  }
  /**
   * Deletes a poll for selected month
   * @param month
   * @param year
   * @param token
   */
  public deletePoll = async (
    year: number,
    month: number,
    token: string
  ): Promise<void> => {
    try {
      await axios.delete(
        `/api/admin/poll/${year}/${month}`,
        {
          headers: {
            'XSRF-TOKEN': token,
          },
        }
      )
      toast.success('Poll succesfully deleted !')
    } catch (e) {
      toast.error('Failed to delete poll')
      console.log(e)
    }
  }
  /**
   * Deletes a Monthly News for selected month
   * @param year
   * @param month
   * @param token
   */
  public deleteMonthlyNews = async (
    year: number,
    month: number,
    token: string
  ): Promise<void> => {
    try {
      await axios.delete(
        `/api/admin/monthlyNews/${year}/${month}`,
        {
          headers: {
            'XSRF-TOKEN': token,
          },
        }
      )
      toast.success('Monthly news succesfully deleted !')
    } catch (e) {
      toast.error('Failed to delete monthly news')
      console.log(e)
    }
  }
}