import { gql } from '@apollo/client'

// =============================================================================
// 📦 МУТАЦИИ ДЛЯ ТОВАРНЫХ ПОСТАВОК СЕЛЛЕРА V2
// =============================================================================

export const CREATE_SELLER_GOODS_SUPPLY = gql`
  mutation CreateSellerGoodsSupply($input: CreateSellerGoodsSupplyInput!) {
    createSellerGoodsSupply(input: $input) {
      success
      message
      supplyOrder {
        id
        status
        createdAt
        updatedAt
        
        # Основные данные
        sellerId
        seller {
          id
          name
          inn
        }
        
        fulfillmentCenterId
        fulfillmentCenter {
          id
          name
          address
        }
        
        supplierId
        supplier {
          id
          name
          inn
        }
        
        logisticsPartnerId
        logisticsPartner {
          id
          name
        }
        
        # Даты и статусы
        requestedDeliveryDate
        estimatedDeliveryDate
        deliveredAt
        shippedAt
        
        # Финансы
        totalCostWithDelivery
        actualDeliveryCost
        
        # Рецептура (нормализованная)
        recipeItems {
          id
          productId
          quantity
          recipeType
          product {
            id
            name
            article
            price
          }
        }
        
        # Метаданные
        notes
        supplierNotes
        receiptNotes
        trackingNumber
        packagesCount
        estimatedVolume
      }
    }
  }
`

export const UPDATE_SELLER_GOODS_SUPPLY_STATUS = gql`
  mutation UpdateSellerGoodsSupplyStatus($id: ID!, $status: SellerSupplyOrderStatus!, $notes: String) {
    updateSellerGoodsSupplyStatus(id: $id, status: $status, notes: $notes) {
      id
      status
      updatedAt
      supplierApprovedAt
      shippedAt
      deliveredAt
      supplierNotes
      receiptNotes
    }
  }
`

export const UPDATE_SUPPLY_VOLUME_V2 = gql`
  mutation UpdateSupplyVolumeV2($id: ID!, $volume: Float) {
    updateSellerGoodsSupplyVolume(id: $id, volume: $volume) {
      id
      estimatedVolume
      updatedAt
    }
  }
`

export const UPDATE_SUPPLY_PACKAGES_V2 = gql`
  mutation UpdateSupplyPackagesV2($id: ID!, $packagesCount: Int) {
    updateSellerGoodsSupplyPackages(id: $id, packagesCount: $packagesCount) {
      id
      packagesCount
      updatedAt
    }
  }
`

export const CANCEL_SELLER_GOODS_SUPPLY = gql`
  mutation CancelSellerGoodsSupply($id: ID!) {
    cancelSellerGoodsSupply(id: $id) {
      id
      status
      updatedAt
    }
  }
`

// =============================================================================
// 📊 ЗАПРОСЫ ДЛЯ ТОВАРНЫХ ПОСТАВОК V2
// =============================================================================

export const GET_MY_SELLER_GOODS_SUPPLIES = gql`
  query GetMySellerGoodsSupplies {
    mySellerGoodsSupplies {
      id
      status
      createdAt
      updatedAt
      
      seller {
        id
        name
      }
      
      fulfillmentCenter {
        id
        name
      }
      
      supplier {
        id
        name
      }
      
      requestedDeliveryDate
      deliveredAt
      
      totalCostWithDelivery
      
      recipeItems {
        id
        productId
        quantity
        recipeType
        product {
          id
          name
          article
          price
        }
      }
    }
  }
`

export const GET_MY_SELLER_GOODS_SUPPLY_REQUESTS = gql`
  query GetMySellerGoodsSupplyRequests {
    mySellerGoodsSupplyRequests {
      id
      status
      sellerId
      seller {
        id
        name
        inn
      }
      fulfillmentCenterId
      fulfillmentCenter {
        id
        name
        inn
      }
      supplierId
      supplier {
        id
        name
        inn
      }
      logisticsPartnerId
      logisticsPartner {
        id
        name
        inn
      }
      requestedDeliveryDate
      estimatedDeliveryDate
      shippedAt
      deliveredAt
      supplierApprovedAt
      receivedById
      receivedBy {
        id
        managerName
        phone
      }
      notes
      supplierNotes
      receiptNotes
      totalCostWithDelivery
      packagesCount
      estimatedVolume
      trackingNumber
      
      recipeItems {
        id
        productId
        quantity
        recipeType
        product {
          id
          name
          article
          price
          mainImage
        }
      }
      
      createdAt
      updatedAt
    }
  }
`

export const GET_SELLER_GOODS_INVENTORY = gql`
  query GetSellerGoodsInventory {
    mySellerGoodsInventory {
      id
      
      seller {
        id
        name
      }
      
      fulfillmentCenter {
        id
        name
      }
      
      product {
        id
        name
        article
      }
      
      currentStock
      reservedStock
      inPreparationStock
      totalReceived
      totalShipped
      
      averageCost
      salePrice
      
      lastSupplyDate
      lastShipDate
      
      notes
    }
  }
`