import gql from "graphql-tag";

export const F_TRIGGER = gql`
  fragment Ftrigger on Trigger {
    _id
    createdAt
    updatedAt
    expiresAt
    event
    sender {
      _id
      name
    }
    tags {
      key
      value
    }
    template {
      _id
      name
    }
    isEnabled
  }
`;

export const F_POINT = gql`
  fragment Fpoint on Point {
    remains
    chargeType
    postpayOption {
      lastPayDate
      paymentDate
      lastPayAmount
    }
    prepayOption {
      lowerLimit
      amount
    }
  }
`;

export const F_TEMPLATE = gql`
  fragment Ftemplate on Template {
    _id
    createdAt
    updatedAt
    expiresAt
    name
    description
    content
    replacementFields
  }
`;

export const F_SENDER = gql`
  fragment Fsender on Sender {
    _id
    createdAt
    updatedAt
    expiresAt
    phoneNumber
    name
    verified
    authInfo {
      status
      authDate
    }
  }
`;

export const SENDE = gql`
  mutation send($input: SendInput!) {
    Send(input: $input) {
      ok
      errors
    }
  }
`;

export const TRIGGER_CREATE = gql`
  mutation triggerCreate($input: TriggerUpsertInput!) {
    TriggerCreate(input: $input) {
      ok
      errors
    }
  }
`;

export const UPDATE_TRIGGER = gql`
  mutation triggerUpdate($input: TriggerUpsertInput!, $triggerId: ObjectId!) {
    TriggerUpdate(input: $input, triggerId: $triggerId) {
      ok
      errors
    }
  }
`;

export const UPDATE_TEMPLATE = gql`
  mutation templateUpdate(
    $params: TemplateUpsertInput!
    $templateId: ObjectId!
  ) {
    TemplateUpdate(params: $params, templateId: $templateId) {
      ok
      errors
      data {
        _id
      }
    }
  }
`;

export const DELETE_TEMPLATE = gql`
  mutation templateDelete($templateId: ObjectId!) {
    TemplateDelete(templateId: $templateId) {
      ok
      errors
      data {
        _id
      }
    }
  }
`;

export const CREATE_TEMPLATE = gql`
  mutation templateCreate($input: TemplateUpsertInput!) {
    TemplateCreate(input: $input) {
      ok
      errors
      data {
        _id
      }
    }
  }
`;

// 이거 내생각에는
// SMS 클래스에서 쿼리한걸 공통으로 써도 되지 않을까?
// 다음에 리팩토링 하면 생각좀 해봐야할듯
// 지금은 이걸해서 얻는게 너무 없다
export const GET_POINT = gql`
  query getPoint {
    MyManager {
      ok
      errors
      data {
        point {
          ...Fpoint
        }
      }
    }
  }
  ${F_POINT}
`;

export const GET_SMS_MANAGER = gql`
  query myManager {
    MyManager {
      ok
      errors
      data {
        _id
        createdAt
        updatedAt
        expiresAt
        key
        roles
        senders {
          ...Fsender
        }
        point {
          ...Fpoint
        }
        triggerEventList
        triggers {
          ...Ftrigger
        }
        templates {
          ...Ftemplate
          triggers {
            ...Ftrigger
          }
        }
      }
    }
  }
  ${F_SENDER}
  ${F_TEMPLATE}
  ${F_TRIGGER}
  ${F_POINT}
`;

export const SENDER_CREATE = gql`
  mutation senderCreate($input: SenderUpsertInput!) {
    SenderCreate(input: $input) {
      ok
      errors
    }
  }
`;

export const TRIGGER_EDIT = gql`
  mutation triggerListEditToTemplate(
    $input: TriggerListEditToTemplateInput!
    $templateId: ObjectId!
  ) {
    TriggerListEditToTemplate(input: $input, templateId: $templateId) {
      ok
      errors
    }
  }
`;

export const GET_HISTORY = gql`
  query history($filter: HistoryFilter!, $pageArgs: HistoryArgs!) {
    History(filter: $filter, pageArgs: $pageArgs) {
      ok
      errors
      data {
        items {
          _id
          createdAt
          updatedAt
          expiresAt
          amount
          description
          isRefunded
          itemType
          ok
          aligoMid
          type
          receivers
          successCount
          errorCount
          template {
            name
          }
          sender {
            phoneNumber
          }
          trigger {
            event
          }
        }
        currentPage
        totalPage
        rowCount
        totalItemCount
      }
    }
  }
`;
