from django.urls import path, include
from rest_framework.routers import DefaultRouter
from .views import (
    stock_change_views,
    healthy,
    user_info,
    inventory,
    product_image,
    stateflow,
    users,
    print_count,
    tiia,
)
from .views.business.purchase import views as purchase_views
from .views.business.sales import views as sales_views
from .views.business.payment import views as payment_views
from .views.business.receipt import views as receipt_views
from .views.business.balance import views as balance_views
from .views.business.purchase_return import views as purchase_return_views
from .views.business.sales_return import views as sales_return_views
from .views.business.statements import views as statement_views
from .views.business.pre_sales import views as pre_sales_views
from .views.business.pre_purchase import views as pre_purchase_views
from .views.stock_change_views.snapshot import StockSnapshotListView
from .views.printing.views import (
    PrintingOrderViewSet,
    PrintingJobViewSet,
    PlateOrderViewSet,
)
from .views.upload import UploadFileViewSet
from .views.products import ProductQuickViewSet
from .views.parameters import StateParameterViewSet
from .views.users import CreateUserWithProfileView
from .views.mingdaoyun import MDYPlateOrderStagingViewSet
from .views.shipment import (
    SalesItemDetailView,
    SalesItemRebuildView,
    SalesItemByCustomerView,
    SalesItemByPrintingOrderView,
    SalesItemCreateView,
    ShipmentStatusUpdateView,
    ShipmentDeliveryBindShipmentsView,
    ShipmentDeliveryCancelView,
    ShipmentDeliveryDetailView,
    ShipmentDeliveryListCreateView,
    ShipmentDeliveryStatusUpdateView,
    ShipmentSalesItemCustomerListView,
    ShipmentListCreateView,
    ShipmentDetailView,
    ShipmentExternalCreateView,
)
from .views.settlement.views import PlateOrderSummaryView

# 创建 DRF Router for Stateflow
stateflow_router = DefaultRouter()
stateflow_router.register(r"states", stateflow.StateViewSet, basename="state")
stateflow_router.register(r"processes", stateflow.ProcessViewSet, basename="process")
stateflow_router.register(
    r"business-objects", stateflow.BusinessObjectViewSet, basename="business-object"
)

# 创建主 Router
main_router = DefaultRouter()
main_router.register(
    r"printing-orders", PrintingOrderViewSet, basename="printing-order"
)
main_router.register(r"printing-jobs", PrintingJobViewSet, basename="printing-job")
main_router.register(r"plate-orders", PlateOrderViewSet, basename="plate-order")
main_router.register(r"upload", UploadFileViewSet, basename="upload")
main_router.register(r"products/quick", ProductQuickViewSet, basename="product-quick")
main_router.register(r"parameters", StateParameterViewSet, basename="parameter")
main_router.register(
    r"mdy-plate-order-staging",
    MDYPlateOrderStagingViewSet,
    basename="mdy-plate-order-staging",
)

urlpatterns = [
    # 库存变动相关API
    path(
        "stock-snapshots/", StockSnapshotListView.as_view(), name="list_stock_snapshots"
    ),
    path(
        "stock-changes/",
        stock_change_views.list_stock_changes,
        name="list_stock_changes",
    ),
    path(
        "stock-change-details/",
        stock_change_views.list_stock_change_details,
        name="list_stock_change_details",
    ),
    path(
        "stock-change/",
        stock_change_views.create_full_stock_change,
        name="create_full_stock_change",
    ),
    path(
        "stock-change/relaxed/",
        stock_change_views.create_relaxed_stock_change,
        name="create_relaxed_stock_change",
    ),
    path(
        "stock-change/restrict/",
        stock_change_views.create_restrict_stock_change,
        name="create_restrict_stock_change",
    ),
    path(
        "stock-change/transfer/",
        stock_change_views.transfer_stock_change,
        name="stock_transfer",
    ),
    path(
        "stock-change/<int:record_id>/",
        stock_change_views.get_stock_change,
        name="get_stock_change",
    ),
    path(
        "stock-change/<int:record_id>/finish/",
        stock_change_views.finish_stock_change,
        name="finish_stock_change",
    ),
    path(
        "stock-change/<int:record_id>/offset/",
        stock_change_views.offset_stock_change,
        name="offset_stock_change",
    ),
    path(
        "set-merchant-auto-complete-stock-change/",
        stock_change_views.set_merchant_auto_complete_stock_change,
        name="set_merchant_auto_complete_stock_change",
    ),
    # 用户信息 API
    path("user-info/", user_info.user_info, name="user_info"),
    # 用户创建 API
    path(
        "users/create/",
        CreateUserWithProfileView.as_view(),
        name="create_user_with_profile",
    ),
    # 库存查询 API
    path("inventory/", inventory.InventoryAPIView.as_view(), name="inventory"),
    path(
        "purchase-orders/",
        purchase_views.PurchaseOrderView.as_view(),
        name="purchase_orders",
    ),
    path(
        "purchase-orders/<int:pk>/",
        purchase_views.PurchaseOrderDetailView.as_view(),
        name="purchase_order_detail",
    ),
    path(
        "purchase-orders/<int:pk>/review/",
        purchase_views.PurchaseOrderReviewView.as_view(),
        name="purchase_order_review",
    ),
    path(
        "purchase-orders/<int:pk>/red-flush/",
        purchase_views.PurchaseOrderRedFlushView.as_view(),
        name="purchase_order_red_flush",
    ),
    path(
        "purchase-orders/<int:pk>/bind-stock-change/",
        purchase_views.PurchaseOrderBindStockChangeView.as_view(),
        name="purchase_order_bind_stock_change",
    ),
    path(
        "purchase-return-orders/",
        purchase_return_views.PurchaseReturnOrderView.as_view(),
        name="purchase_return_orders",
    ),
    path(
        "purchase-return-orders/<int:pk>/",
        purchase_return_views.PurchaseReturnOrderDetailView.as_view(),
        name="purchase_return_order_detail",
    ),
    path(
        "purchase-return-orders/<int:pk>/review/",
        purchase_return_views.PurchaseReturnOrderReviewView.as_view(),
        name="purchase_return_order_review",
    ),
    path(
        "purchase-return-orders/<int:pk>/red-flush/",
        purchase_return_views.PurchaseReturnOrderRedFlushView.as_view(),
        name="purchase_return_order_red_flush",
    ),
    path("sales-orders/", sales_views.SalesOrderView.as_view(), name="sales_orders"),
    path(
        "sales-orders/<int:pk>/",
        sales_views.SalesOrderDetailView.as_view(),
        name="sales_order_detail",
    ),
    path(
        "sales-orders/<int:pk>/review/",
        sales_views.SalesOrderReviewView.as_view(),
        name="sales_order_review",
    ),
    path(
        "sales-orders/<int:pk>/red-flush/",
        sales_views.SalesOrderRedFlushView.as_view(),
        name="sales_order_red_flush",
    ),
    path(
        "sales-return-orders/",
        sales_return_views.SalesReturnOrderView.as_view(),
        name="sales_return_orders",
    ),
    path(
        "sales-return-orders/<int:pk>/",
        sales_return_views.SalesReturnOrderDetailView.as_view(),
        name="sales_return_order_detail",
    ),
    path(
        "sales-return-orders/<int:pk>/review/",
        sales_return_views.SalesReturnOrderReviewView.as_view(),
        name="sales_return_order_review",
    ),
    path(
        "sales-return-orders/<int:pk>/red-flush/",
        sales_return_views.SalesReturnOrderRedFlushView.as_view(),
        name="sales_return_order_red_flush",
    ),
    path(
        "pre-sales-orders/",
        pre_sales_views.PreSalesOrderView.as_view(),
        name="pre_sales_orders",
    ),
    path(
        "pre-sales-orders/<int:pk>/",
        pre_sales_views.PreSalesOrderDetailView.as_view(),
        name="pre_sales_order_detail",
    ),
    path(
        "pre-sales-orders/<int:pk>/convert-to-sales/",
        pre_sales_views.PreSalesOrderConvertToSalesOrderView.as_view(),
        name="pre_sales_order_convert_to_sales",
    ),
    path(
        "pre-sales-order-items/<int:item_id>/allocations/",
        pre_sales_views.PreSalesOrderItemAllocationView.as_view(),
        name="pre_sales_order_item_allocations",
    ),
    path(
        "pre-sales-order-items/<int:item_id>/allocations/<int:pk>/",
        pre_sales_views.PreSalesOrderItemAllocationDetailView.as_view(),
        name="pre_sales_order_item_allocation_detail",
    ),
    path(
        "pre-purchase-orders/",
        pre_purchase_views.PrePurchaseOrderView.as_view(),
        name="pre_purchase_orders",
    ),
    path(
        "pre-purchase-orders/<int:pk>/",
        pre_purchase_views.PrePurchaseOrderDetailView.as_view(),
        name="pre_purchase_order_detail",
    ),
    path(
        "payment-orders/",
        payment_views.PaymentOrderView.as_view(),
        name="payment_orders",
    ),
    path(
        "payment-orders/<int:pk>/review/",
        payment_views.PaymentOrderReviewView.as_view(),
        name="payment_order_review",
    ),
    path(
        "payment-orders/<int:pk>/red-flush/",
        payment_views.PaymentOrderRedFlushView.as_view(),
        name="payment_order_red_flush",
    ),
    path(
        "receipt-orders/",
        receipt_views.ReceiptOrderView.as_view(),
        name="receipt_orders",
    ),
    path(
        "receipt-orders/<int:pk>/review/",
        receipt_views.ReceiptOrderReviewView.as_view(),
        name="receipt_order_review",
    ),
    path(
        "receipt-orders/<int:pk>/red-flush/",
        receipt_views.ReceiptOrderRedFlushView.as_view(),
        name="receipt_order_red_flush",
    ),
    path(
        "customers/<int:customer_id>/balance/",
        balance_views.CustomerBalanceView.as_view(),
        name="customer_balance",
    ),
    path(
        "suppliers/<int:supplier_id>/balance/",
        balance_views.SupplierBalanceView.as_view(),
        name="supplier_balance",
    ),
    path(
        "customers/<int:customer_id>/statements/",
        statement_views.CustomerStatementView.as_view(),
        name="customer_statements",
    ),
    path(
        "suppliers/<int:supplier_id>/statements/",
        statement_views.SupplierStatementView.as_view(),
        name="supplier_statements",
    ),
    path(
        "statements/record/",
        statement_views.StatementRecordView.as_view(),
        name="statement_record",
    ),
    path("health/", healthy.HealthCheckView.as_view(), name="health_check"),
    path(
        "print-count/delta/", print_count.adjust_print_count, name="print_count_delta"
    ),
    # 产品图片上传 API
    path(
        "products/<int:product_id>/image/",
        product_image.ProductImageUploadView.as_view(),
        name="product_image_upload",
    ),
    # Tencent Cloud (TIIA) API
    path(
        "tiia/search-image/",
        tiia.TiiaSearchImageView.as_view(),
        name="tiia_search_image",
    ),
    # Stateflow API (使用 Router)
    path("stateflow/", include(stateflow_router.urls)),
    # Shipment API
    path(
        "shipment/shipments/",
        ShipmentListCreateView.as_view(),
        name="shipment_list_create",
    ),
    path(
        "shipment/shipments/<int:pk>/",
        ShipmentDetailView.as_view(),
        name="shipment_detail",
    ),
    path(
        "shipment/shipments/<int:pk>/status/",
        ShipmentStatusUpdateView.as_view(),
        name="shipment_status_update",
    ),
    path(
        "shipment/shipments/external/",
        ShipmentExternalCreateView.as_view(),
        name="shipment_create_external",
    ),
    path(
        "shipment/deliveries/",
        ShipmentDeliveryListCreateView.as_view(),
        name="shipment_delivery_list_create",
    ),
    path(
        "shipment/deliveries/<int:pk>/",
        ShipmentDeliveryDetailView.as_view(),
        name="shipment_delivery_detail",
    ),
    path(
        "shipment/deliveries/<int:pk>/status/",
        ShipmentDeliveryStatusUpdateView.as_view(),
        name="shipment_delivery_status_update",
    ),
    path(
        "shipment/deliveries/<int:pk>/cancel/",
        ShipmentDeliveryCancelView.as_view(),
        name="shipment_delivery_cancel",
    ),
    path(
        "shipment/deliveries/<int:pk>/bind-shipments/",
        ShipmentDeliveryBindShipmentsView.as_view(),
        name="shipment_delivery_bind_shipments",
    ),
    path(
        "shipment/sales-items/customers/",
        ShipmentSalesItemCustomerListView.as_view(),
        name="sales_item_customers",
    ),
    path(
        "shipment/sales-items/by-customer/<int:customer_id>/",
        SalesItemByCustomerView.as_view(),
        name="sales_items_by_customer",
    ),
    path(
        "shipment/sales-items/by-printing-order/<str:printing_order_id>/",
        SalesItemByPrintingOrderView.as_view(),
        name="sales_items_by_printing_order",
    ),
    path(
        "shipment/sales-items/<int:pk>/",
        SalesItemDetailView.as_view(),
        name="sales_item_detail",
    ),
    path(
        "shipment/sales-items/<int:pk>/rebuild/",
        SalesItemRebuildView.as_view(),
        name="sales_item_rebuild",
    ),
    path(
        "shipment/sales-items/", SalesItemCreateView.as_view(), name="sales_item_create"
    ),
    # Settlement API
    path(
        "settlement/plate-orders/summary/",
        PlateOrderSummaryView.as_view(),
        name="plate_order_summary",
    ),
    # 主 Router (printing-orders 等)
    path("", include(main_router.urls)),
]
