import { gql } from "graphql-tag";

export const SEND_SMS_CODE = gql`
  mutation SendSmsCode($phone: String!) {
    sendSmsCode(phone: $phone) {
      success
      message
    }
  }
`;

export const VERIFY_SMS_CODE = gql`
  mutation VerifySmsCode($phone: String!, $code: String!) {
    verifySmsCode(phone: $phone, code: $code) {
      success
      message
      token
      user {
        id
        phone
        organization {
          id
          inn
          kpp
          name
          fullName
          address
          addressFull
          ogrn
          ogrnDate
          type
          status
          actualityDate
          registrationDate
          liquidationDate
          managementName
          managementPost
          opfCode
          opfFull
          opfShort
          okato
          oktmo
          okpo
          okved
          employeeCount
          revenue
          taxSystem
          phones
          emails
          apiKeys {
            id
            marketplace
            isActive
          }
        }
      }
    }
  }
`;

export const VERIFY_INN = gql`
  mutation VerifyInn($inn: String!) {
    verifyInn(inn: $inn) {
      success
      message
      organization {
        name
        fullName
        address
        isActive
      }
    }
  }
`;

export const REGISTER_FULFILLMENT_ORGANIZATION = gql`
  mutation RegisterFulfillmentOrganization(
    $input: FulfillmentRegistrationInput!
  ) {
    registerFulfillmentOrganization(input: $input) {
      success
      message
      user {
        id
        phone
        organization {
          id
          inn
          kpp
          name
          fullName
          address
          addressFull
          ogrn
          ogrnDate
          type
          status
          actualityDate
          registrationDate
          liquidationDate
          managementName
          managementPost
          opfCode
          opfFull
          opfShort
          okato
          oktmo
          okpo
          okved
          employeeCount
          revenue
          taxSystem
          phones
          emails
          apiKeys {
            id
            marketplace
            isActive
          }
        }
      }
    }
  }
`;

export const REGISTER_SELLER_ORGANIZATION = gql`
  mutation RegisterSellerOrganization($input: SellerRegistrationInput!) {
    registerSellerOrganization(input: $input) {
      success
      message
      user {
        id
        phone
        organization {
          id
          inn
          kpp
          name
          fullName
          address
          addressFull
          ogrn
          ogrnDate
          type
          status
          actualityDate
          registrationDate
          liquidationDate
          managementName
          managementPost
          opfCode
          opfFull
          opfShort
          okato
          oktmo
          okpo
          okved
          employeeCount
          revenue
          taxSystem
          phones
          emails
          apiKeys {
            id
            marketplace
            isActive
          }
        }
      }
    }
  }
`;

export const ADD_MARKETPLACE_API_KEY = gql`
  mutation AddMarketplaceApiKey($input: MarketplaceApiKeyInput!) {
    addMarketplaceApiKey(input: $input) {
      success
      message
      apiKey {
        id
        marketplace
        apiKey
        isActive
        validationData
      }
    }
  }
`;

export const REMOVE_MARKETPLACE_API_KEY = gql`
  mutation RemoveMarketplaceApiKey($marketplace: MarketplaceType!) {
    removeMarketplaceApiKey(marketplace: $marketplace)
  }
`;

export const UPDATE_USER_PROFILE = gql`
  mutation UpdateUserProfile($input: UpdateUserProfileInput!) {
    updateUserProfile(input: $input) {
      success
      message
      user {
        id
        phone
        avatar
        managerName
        organization {
          id
          inn
          kpp
          name
          fullName
          address
          addressFull
          ogrn
          ogrnDate
          type
          status
          actualityDate
          registrationDate
          liquidationDate
          managementName
          managementPost
          opfCode
          opfFull
          opfShort
          okato
          oktmo
          okpo
          okved
          employeeCount
          revenue
          taxSystem
          phones
          emails
          apiKeys {
            id
            marketplace
            isActive
          }
        }
      }
    }
  }
`;

export const UPDATE_ORGANIZATION_BY_INN = gql`
  mutation UpdateOrganizationByInn($inn: String!) {
    updateOrganizationByInn(inn: $inn) {
      success
      message
      user {
        id
        phone
        organization {
          id
          inn
          kpp
          name
          fullName
          address
          addressFull
          ogrn
          ogrnDate
          type
          status
          actualityDate
          registrationDate
          liquidationDate
          managementName
          managementPost
          opfCode
          opfFull
          opfShort
          okato
          oktmo
          okpo
          okved
          employeeCount
          revenue
          taxSystem
          phones
          emails
          apiKeys {
            id
            marketplace
            isActive
          }
        }
      }
    }
  }
`;

// Мутации для контрагентов
export const SEND_COUNTERPARTY_REQUEST = gql`
  mutation SendCounterpartyRequest($organizationId: ID!, $message: String) {
    sendCounterpartyRequest(
      organizationId: $organizationId
      message: $message
    ) {
      success
      message
      request {
        id
        status
        message
        createdAt
        sender {
          id
          inn
          name
          fullName
          type
        }
        receiver {
          id
          inn
          name
          fullName
          type
        }
      }
    }
  }
`;

export const RESPOND_TO_COUNTERPARTY_REQUEST = gql`
  mutation RespondToCounterpartyRequest($requestId: ID!, $accept: Boolean!) {
    respondToCounterpartyRequest(requestId: $requestId, accept: $accept) {
      success
      message
      request {
        id
        status
        message
        createdAt
        sender {
          id
          inn
          name
          fullName
          type
        }
        receiver {
          id
          inn
          name
          fullName
          type
        }
      }
    }
  }
`;

export const CANCEL_COUNTERPARTY_REQUEST = gql`
  mutation CancelCounterpartyRequest($requestId: ID!) {
    cancelCounterpartyRequest(requestId: $requestId)
  }
`;

export const REMOVE_COUNTERPARTY = gql`
  mutation RemoveCounterparty($organizationId: ID!) {
    removeCounterparty(organizationId: $organizationId)
  }
`;

// Мутации для сообщений
export const SEND_MESSAGE = gql`
  mutation SendMessage(
    $receiverOrganizationId: ID!
    $content: String!
    $type: MessageType = TEXT
  ) {
    sendMessage(
      receiverOrganizationId: $receiverOrganizationId
      content: $content
      type: $type
    ) {
      success
      message
      messageData {
        id
        content
        type
        voiceUrl
        voiceDuration
        fileUrl
        fileName
        fileSize
        fileType
        senderId
        senderOrganization {
          id
          name
          fullName
          type
          users {
            id
            avatar
            managerName
          }
        }
        receiverOrganization {
          id
          name
          fullName
          type
          users {
            id
            avatar
            managerName
          }
        }
        isRead
        createdAt
        updatedAt
      }
    }
  }
`;

export const SEND_VOICE_MESSAGE = gql`
  mutation SendVoiceMessage(
    $receiverOrganizationId: ID!
    $voiceUrl: String!
    $voiceDuration: Int!
  ) {
    sendVoiceMessage(
      receiverOrganizationId: $receiverOrganizationId
      voiceUrl: $voiceUrl
      voiceDuration: $voiceDuration
    ) {
      success
      message
      messageData {
        id
        content
        type
        voiceUrl
        voiceDuration
        fileUrl
        fileName
        fileSize
        fileType
        senderId
        senderOrganization {
          id
          name
          fullName
          type
          users {
            id
            avatar
            managerName
          }
        }
        receiverOrganization {
          id
          name
          fullName
          type
          users {
            id
            avatar
            managerName
          }
        }
        isRead
        createdAt
        updatedAt
      }
    }
  }
`;

export const SEND_IMAGE_MESSAGE = gql`
  mutation SendImageMessage(
    $receiverOrganizationId: ID!
    $fileUrl: String!
    $fileName: String!
    $fileSize: Int!
    $fileType: String!
  ) {
    sendImageMessage(
      receiverOrganizationId: $receiverOrganizationId
      fileUrl: $fileUrl
      fileName: $fileName
      fileSize: $fileSize
      fileType: $fileType
    ) {
      success
      message
      messageData {
        id
        content
        type
        voiceUrl
        voiceDuration
        fileUrl
        fileName
        fileSize
        fileType
        senderId
        senderOrganization {
          id
          name
          fullName
          type
          users {
            id
            avatar
            managerName
          }
        }
        receiverOrganization {
          id
          name
          fullName
          type
          users {
            id
            avatar
            managerName
          }
        }
        isRead
        createdAt
        updatedAt
      }
    }
  }
`;

export const SEND_FILE_MESSAGE = gql`
  mutation SendFileMessage(
    $receiverOrganizationId: ID!
    $fileUrl: String!
    $fileName: String!
    $fileSize: Int!
    $fileType: String!
  ) {
    sendFileMessage(
      receiverOrganizationId: $receiverOrganizationId
      fileUrl: $fileUrl
      fileName: $fileName
      fileSize: $fileSize
      fileType: $fileType
    ) {
      success
      message
      messageData {
        id
        content
        type
        voiceUrl
        voiceDuration
        fileUrl
        fileName
        fileSize
        fileType
        senderId
        senderOrganization {
          id
          name
          fullName
          type
          users {
            id
            avatar
            managerName
          }
        }
        receiverOrganization {
          id
          name
          fullName
          type
          users {
            id
            avatar
            managerName
          }
        }
        isRead
        createdAt
        updatedAt
      }
    }
  }
`;

export const MARK_MESSAGES_AS_READ = gql`
  mutation MarkMessagesAsRead($conversationId: ID!) {
    markMessagesAsRead(conversationId: $conversationId)
  }
`;

// Мутации для услуг
export const CREATE_SERVICE = gql`
  mutation CreateService($input: ServiceInput!) {
    createService(input: $input) {
      success
      message
      service {
        id
        name
        description
        price
        imageUrl
        createdAt
        updatedAt
      }
    }
  }
`;

export const UPDATE_SERVICE = gql`
  mutation UpdateService($id: ID!, $input: ServiceInput!) {
    updateService(id: $id, input: $input) {
      success
      message
      service {
        id
        name
        description
        price
        imageUrl
        createdAt
        updatedAt
      }
    }
  }
`;

export const DELETE_SERVICE = gql`
  mutation DeleteService($id: ID!) {
    deleteService(id: $id)
  }
`;

// Мутации для расходников
export const CREATE_SUPPLY = gql`
  mutation CreateSupply($input: SupplyInput!) {
    createSupply(input: $input) {
      success
      message
      supply {
        id
        name
        description
        price
        quantity
        unit
        category
        status
        date
        supplier
        minStock
        currentStock
        imageUrl
        createdAt
        updatedAt
      }
    }
  }
`;

export const UPDATE_SUPPLY = gql`
  mutation UpdateSupply($id: ID!, $input: SupplyInput!) {
    updateSupply(id: $id, input: $input) {
      success
      message
      supply {
        id
        name
        description
        price
        quantity
        unit
        category
        status
        date
        supplier
        minStock
        currentStock
        imageUrl
        createdAt
        updatedAt
      }
    }
  }
`;

export const DELETE_SUPPLY = gql`
  mutation DeleteSupply($id: ID!) {
    deleteSupply(id: $id)
  }
`;

// Мутация для заказа поставки расходников
export const CREATE_SUPPLY_ORDER = gql`
  mutation CreateSupplyOrder($input: SupplyOrderInput!) {
    createSupplyOrder(input: $input) {
      success
      message
      order {
        id
        partnerId
        deliveryDate
        status
        totalAmount
        totalItems
        createdAt
        partner {
          id
          inn
          name
          fullName
          address
          phones
          emails
        }
        items {
          id
          quantity
          price
          totalPrice
          product {
            id
            name
            article
            description
            price
            quantity
            images
            mainImage
            category {
              id
              name
            }
          }
        }
      }
    }
  }
`;

// Мутации для логистики
export const CREATE_LOGISTICS = gql`
  mutation CreateLogistics($input: LogisticsInput!) {
    createLogistics(input: $input) {
      success
      message
      logistics {
        id
        fromLocation
        toLocation
        priceUnder1m3
        priceOver1m3
        description
        createdAt
        updatedAt
      }
    }
  }
`;

export const UPDATE_LOGISTICS = gql`
  mutation UpdateLogistics($id: ID!, $input: LogisticsInput!) {
    updateLogistics(id: $id, input: $input) {
      success
      message
      logistics {
        id
        fromLocation
        toLocation
        priceUnder1m3
        priceOver1m3
        description
        createdAt
        updatedAt
      }
    }
  }
`;

export const DELETE_LOGISTICS = gql`
  mutation DeleteLogistics($id: ID!) {
    deleteLogistics(id: $id)
  }
`;

// Мутации для товаров оптовика
export const CREATE_PRODUCT = gql`
  mutation CreateProduct($input: ProductInput!) {
    createProduct(input: $input) {
      success
      message
      product {
        id
        name
        article
        description
        price
        quantity
        category {
          id
          name
        }
        brand
        color
        size
        weight
        dimensions
        material
        images
        mainImage
        isActive
        createdAt
        updatedAt
      }
    }
  }
`;

export const UPDATE_PRODUCT = gql`
  mutation UpdateProduct($id: ID!, $input: ProductInput!) {
    updateProduct(id: $id, input: $input) {
      success
      message
      product {
        id
        name
        article
        description
        price
        quantity
        category {
          id
          name
        }
        brand
        color
        size
        weight
        dimensions
        material
        images
        mainImage
        isActive
        createdAt
        updatedAt
      }
    }
  }
`;

export const DELETE_PRODUCT = gql`
  mutation DeleteProduct($id: ID!) {
    deleteProduct(id: $id)
  }
`;

// Мутации для корзины
export const ADD_TO_CART = gql`
  mutation AddToCart($productId: ID!, $quantity: Int = 1) {
    addToCart(productId: $productId, quantity: $quantity) {
      success
      message
      cart {
        id
        totalPrice
        totalItems
        items {
          id
          quantity
          totalPrice
          isAvailable
          availableQuantity
          product {
            id
            name
            article
            price
            quantity
            images
            mainImage
            organization {
              id
              name
              fullName
            }
          }
        }
      }
    }
  }
`;

export const UPDATE_CART_ITEM = gql`
  mutation UpdateCartItem($productId: ID!, $quantity: Int!) {
    updateCartItem(productId: $productId, quantity: $quantity) {
      success
      message
      cart {
        id
        totalPrice
        totalItems
        items {
          id
          quantity
          totalPrice
          isAvailable
          availableQuantity
          product {
            id
            name
            article
            price
            quantity
            images
            mainImage
            organization {
              id
              name
              fullName
            }
          }
        }
      }
    }
  }
`;

export const REMOVE_FROM_CART = gql`
  mutation RemoveFromCart($productId: ID!) {
    removeFromCart(productId: $productId) {
      success
      message
      cart {
        id
        totalPrice
        totalItems
        items {
          id
          quantity
          totalPrice
          isAvailable
          availableQuantity
          product {
            id
            name
            article
            price
            quantity
            images
            mainImage
            organization {
              id
              name
              fullName
            }
          }
        }
      }
    }
  }
`;

export const CLEAR_CART = gql`
  mutation ClearCart {
    clearCart
  }
`;

// Мутации для избранного
export const ADD_TO_FAVORITES = gql`
  mutation AddToFavorites($productId: ID!) {
    addToFavorites(productId: $productId) {
      success
      message
      favorites {
        id
        name
        article
        price
        quantity
        images
        mainImage
        category {
          id
          name
        }
        organization {
          id
          name
          fullName
          inn
        }
      }
    }
  }
`;

export const REMOVE_FROM_FAVORITES = gql`
  mutation RemoveFromFavorites($productId: ID!) {
    removeFromFavorites(productId: $productId) {
      success
      message
      favorites {
        id
        name
        article
        price
        quantity
        images
        mainImage
        category {
          id
          name
        }
        organization {
          id
          name
          fullName
          inn
        }
      }
    }
  }
`;

// Мутации для категорий
export const CREATE_CATEGORY = gql`
  mutation CreateCategory($input: CategoryInput!) {
    createCategory(input: $input) {
      success
      message
      category {
        id
        name
        createdAt
        updatedAt
      }
    }
  }
`;

export const UPDATE_CATEGORY = gql`
  mutation UpdateCategory($id: ID!, $input: CategoryInput!) {
    updateCategory(id: $id, input: $input) {
      success
      message
      category {
        id
        name
        createdAt
        updatedAt
      }
    }
  }
`;

export const DELETE_CATEGORY = gql`
  mutation DeleteCategory($id: ID!) {
    deleteCategory(id: $id)
  }
`;

// Мутации для сотрудников
export const CREATE_EMPLOYEE = gql`
  mutation CreateEmployee($input: CreateEmployeeInput!) {
    createEmployee(input: $input) {
      success
      message
      employee {
        id
        firstName
        lastName
        middleName
        birthDate
        avatar
        position
        department
        hireDate
        salary
        status
        phone
        email
        emergencyContact
        emergencyPhone
        createdAt
        updatedAt
      }
    }
  }
`;

export const UPDATE_EMPLOYEE = gql`
  mutation UpdateEmployee($id: ID!, $input: UpdateEmployeeInput!) {
    updateEmployee(id: $id, input: $input) {
      success
      message
      employee {
        id
        firstName
        lastName
        middleName
        birthDate
        avatar
        passportSeries
        passportNumber
        passportIssued
        passportDate
        address
        position
        department
        hireDate
        salary
        status
        phone
        email
        emergencyContact
        emergencyPhone
        createdAt
        updatedAt
      }
    }
  }
`;

export const DELETE_EMPLOYEE = gql`
  mutation DeleteEmployee($id: ID!) {
    deleteEmployee(id: $id)
  }
`;

export const UPDATE_EMPLOYEE_SCHEDULE = gql`
  mutation UpdateEmployeeSchedule($input: UpdateScheduleInput!) {
    updateEmployeeSchedule(input: $input)
  }
`;

export const CREATE_WILDBERRIES_SUPPLY = gql`
  mutation CreateWildberriesSupply($input: CreateWildberriesSupplyInput!) {
    createWildberriesSupply(input: $input) {
      success
      message
      supply {
        id
        deliveryDate
        status
        totalAmount
        totalItems
        createdAt
      }
    }
  }
`;

// Админ мутации
export const ADMIN_LOGIN = gql`
  mutation AdminLogin($username: String!, $password: String!) {
    adminLogin(username: $username, password: $password) {
      success
      message
      token
      admin {
        id
        username
        email
        isActive
        lastLogin
        createdAt
        updatedAt
      }
    }
  }
`;

export const ADMIN_LOGOUT = gql`
  mutation AdminLogout {
    adminLogout
  }
`;

export const CREATE_SUPPLY_SUPPLIER = gql`
  mutation CreateSupplySupplier($input: CreateSupplySupplierInput!) {
    createSupplySupplier(input: $input) {
      success
      message
      supplier {
        id
        name
        contactName
        phone
        market
        address
        place
        telegram
        createdAt
      }
    }
  }
`;

// Мутация для обновления статуса заказа поставки
export const UPDATE_SUPPLY_ORDER_STATUS = gql`
  mutation UpdateSupplyOrderStatus($id: ID!, $status: SupplyOrderStatus!) {
    updateSupplyOrderStatus(id: $id, status: $status) {
      success
      message
      order {
        id
        status
        deliveryDate
        totalAmount
        totalItems
        partner {
          id
          name
          fullName
        }
        items {
          id
          quantity
          price
          totalPrice
          product {
            id
            name
            article
            description
            price
            quantity
            images
            mainImage
            category {
              id
              name
            }
          }
        }
      }
    }
  }
`;
