// ============ 数据模型定义 ============
use serde::{Deserialize, Serialize};

// 用户相关
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum UserRole {
    Admin,
    Employee,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct User {
    pub id: String,
    pub username: String,
    pub password: String,
    pub real_name: String,
    pub role: UserRole,
    pub permissions: Vec<String>,
    pub created_at: String,
}

#[derive(Debug, Deserialize)]
pub struct LoginRequest {
    pub username: String,
    pub password: String,
}

#[derive(Debug, Serialize)]
pub struct LoginResponse {
    pub success: bool,
    pub message: String,
    pub user: Option<User>,
}

#[derive(Debug, Deserialize)]
pub struct AddUserRequest {
    pub username: String,
    pub password: String,
    pub real_name: String,
    pub role: String,
}

// 商品相关
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Product {
    pub id: String,
    pub name: String,
    pub code: String,
    pub category: String,
    pub unit: String,
    pub unit2: String,
    pub price: f64,
    pub cost: f64,
    pub stock: f64,
    pub stock_unit2: f64,
    pub batch_no: String,
    pub production_date: String,
    pub expiry_date: String,
    pub brand: String,
    pub model: String,
    pub package_type: String,
    pub description: String,
    pub image_path: String,
    pub warehouse_id: String,
    pub location_id: String,
    pub barcode: String,
    pub trace_code: String,
    pub status: String,
    pub category_id: String,
    pub unit_rate: f64,
    pub created_at: String,
}

#[derive(Debug, Deserialize)]
pub struct AddProductRequest {
    pub name: String,
    pub code: String,
    pub category: String,
    pub unit: String,
    pub unit2: String,
    pub price: f64,
    pub cost: f64,
    pub stock: f64,
    pub stock_unit2: f64,
    pub batch_no: String,
    pub production_date: Option<String>,
    pub expiry_date: Option<String>,
    pub brand: String,
    pub model: String,
    pub package_type: String,
    pub description: String,
    pub image_path: String,
    pub barcode: Option<String>,
    pub trace_code: Option<String>,
    pub status: Option<String>,
    pub category_id: Option<String>,
    pub warehouse_id: String,
    pub location_id: String,
    pub unit_rate: f64,
}

// 客户相关
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Customer {
    pub id: String,
    pub name: String,
    pub contact: String,
    pub phone: String,
    pub address: String,
    pub balance: f64,
    pub created_at: String,
}

#[derive(Debug, Deserialize)]
pub struct AddCustomerRequest {
    pub name: String,
    pub contact: String,
    pub phone: String,
    pub address: String,
}

// 供应商相关
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Supplier {
    pub id: String,
    pub name: String,
    pub contact: String,
    pub phone: String,
    pub address: String,
    pub balance: f64,
    pub created_at: String,
}

#[derive(Debug, Deserialize)]
pub struct AddSupplierRequest {
    pub name: String,
    pub contact: String,
    pub phone: String,
    pub address: String,
}

// 销售订单相关
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SaleOrder {
    pub id: String,
    pub order_no: String,
    pub customer_id: String,
    pub customer_name: String,
    pub total_amount: f64,
    pub status: String,
    pub remark: String,
    pub created_at: String,
    pub is_invoiced: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SaleOrderItem {
    pub id: String,
    pub order_id: String,
    pub product_id: String,
    pub product_name: String,
    pub product_code: String,
    pub product_model: String,
    pub quantity: f64,
    pub price: f64,
    pub amount: f64,
    pub batch_no: String,
}

#[derive(Debug, Deserialize)]
pub struct CreateSaleOrderRequest {
    pub customer_id: String,
    pub customer_name: String,
    pub items: Vec<SaleOrderItemInput>,
    pub remark: String,
    pub status: Option<String>,
}

#[derive(Debug, Deserialize)]
pub struct SaleOrderItemInput {
    pub product_id: String,
    pub product_name: String,
    pub quantity: f64,
    pub price: f64,
    pub batch_no: String,
}

// 采购订单相关
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PurchaseOrder {
    pub id: String,
    pub order_no: String,
    pub supplier_id: String,
    pub supplier_name: String,
    pub total_amount: f64,
    pub status: String,
    pub remark: String,
    pub created_at: String,
    pub is_invoiced: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PurchaseOrderItem {
    pub id: String,
    pub order_id: String,
    pub product_id: String,
    pub product_name: String,
    pub product_code: String,
    pub product_model: String,
    pub quantity: f64,
    pub price: f64,
    pub amount: f64,
    pub batch_no: String,
    pub expiry_date: String,
}

#[derive(Debug, Deserialize)]
pub struct CreatePurchaseOrderRequest {
    pub supplier_id: String,
    pub supplier_name: String,
    pub items: Vec<PurchaseOrderItemInput>,
    pub remark: String,
    pub status: Option<String>,
}

#[derive(Debug, Deserialize)]
pub struct PurchaseOrderItemInput {
    pub product_id: String,
    pub product_name: String,
    pub quantity: f64,
    pub price: f64,
    pub batch_no: String,
    pub expiry_date: String,
}

// 库存记录
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InventoryRecord {
    pub id: String,
    pub product_id: String,
    pub product_name: String,
    pub product_code: String,
    pub change_type: String,
    pub quantity: f64,
    pub before_stock: f64,
    pub after_stock: f64,
    pub remark: String,
    pub created_at: String,
}

// 打印模板相关 - 支持按模块类型筛选
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PrintTemplate {
    pub id: String,
    pub template_name: String,
    pub paper_size: String,
    pub paper_width: f64,
    pub paper_height: f64,
    pub margin_top: f64,
    pub margin_bottom: f64,
    pub margin_left: f64,
    pub margin_right: f64,
    pub header_text: String,
    pub footer_text: String,
    pub show_logo: bool,
    pub logo_path: String,
    pub font_size: i32,
    pub is_default: bool,
    pub created_at: String,
    pub template_type: String,
    pub subheader_text: String,
    pub show_company_info: bool,
    pub show_order_no: bool,
    pub show_date: bool,
    pub show_customer: bool,
    pub show_items: bool,
    pub show_total: bool,
    pub show_remark: bool,
    pub show_signature: bool,
    pub show_unit_price: bool,
    pub show_amount: bool,
    pub show_model: bool,
    pub signer_name: String,
    pub auditor_name: String,
    pub confirmer_name: String,
}

#[derive(Debug, Deserialize)]
pub struct CreatePrintTemplateRequest {
    pub template_name: String,
    pub paper_size: String,
    pub paper_width: f64,
    pub paper_height: f64,
    pub margin_top: f64,
    pub margin_bottom: f64,
    pub margin_left: f64,
    pub margin_right: f64,
    pub header_text: String,
    pub footer_text: String,
    pub show_logo: bool,
    pub logo_path: String,
    pub font_size: i32,
    pub is_default: bool,
    pub template_type: String,
    pub subheader_text: Option<String>,
    pub show_company_info: Option<bool>,
    pub show_order_no: Option<bool>,
    pub show_date: Option<bool>,
    pub show_customer: Option<bool>,
    pub show_items: Option<bool>,
    pub show_total: Option<bool>,
    pub show_remark: Option<bool>,
    pub show_signature: Option<bool>,
    pub show_unit_price: Option<bool>,
    pub show_amount: Option<bool>,
    pub show_model: Option<bool>,
    pub signer_name: Option<String>,
    pub auditor_name: Option<String>,
    pub confirmer_name: Option<String>,
}

// 仓库管理相关
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Warehouse {
    pub id: String,
    pub name: String,
    pub code: String,
    pub address: String,
    pub contact: String,
    pub phone: String,
    pub description: String,
    pub is_default: bool,
    pub created_at: String,
}

#[derive(Debug, Deserialize)]
pub struct CreateWarehouseRequest {
    pub name: String,
    pub code: String,
    pub address: String,
    pub contact: String,
    pub phone: String,
    pub description: String,
    pub is_default: bool,
}

// 库位管理相关
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Location {
    pub id: String,
    pub warehouse_id: String,
    pub warehouse_name: String,
    pub code: String,
    pub name: String,
    pub zone: String,
    pub aisle: String,
    pub shelf: String,
    pub level: String,
    pub position: String,
    pub description: String,
    pub created_at: String,
}

#[derive(Debug, Deserialize)]
pub struct CreateLocationRequest {
    pub warehouse_id: String,
    pub code: String,
    pub name: String,
    pub zone: String,
    pub aisle: String,
    pub shelf: String,
    pub level: String,
    pub position: String,
    pub description: String,
}

// 库存调拨相关
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TransferOrder {
    pub id: String,
    pub order_no: String,
    pub from_warehouse_id: String,
    pub from_warehouse_name: String,
    pub to_warehouse_id: String,
    pub to_warehouse_name: String,
    pub status: String,
    pub remark: String,
    pub created_at: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TransferOrderItem {
    pub id: String,
    pub transfer_id: String,
    pub product_id: String,
    pub product_name: String,
    pub quantity: f64,
    pub unit: String,
    pub batch_no: String,
}

#[derive(Debug, Deserialize)]
pub struct CreateTransferOrderRequest {
    pub from_warehouse_id: String,
    pub to_warehouse_id: String,
    pub items: Vec<TransferOrderItemInput>,
    pub remark: String,
}

#[derive(Debug, Deserialize)]
pub struct TransferOrderItemInput {
    pub product_id: String,
    pub product_name: String,
    pub quantity: f64,
    pub unit: String,
    pub batch_no: String,
}

// 产品组装拆分相关
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AssemblyRecord {
    pub id: String,
    pub record_no: String,
    pub assembly_type: String,
    pub product_id: String,
    pub product_name: String,
    pub quantity: f64,
    pub components: String,
    pub remark: String,
    pub created_at: String,
}

#[derive(Debug, Deserialize)]
pub struct CreateAssemblyRequest {
    pub assembly_type: String,
    pub product_id: String,
    pub product_name: String,
    pub quantity: f64,
    pub components: Vec<AssemblyComponentInput>,
    pub remark: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AssemblyComponentInput {
    pub product_id: String,
    pub product_name: String,
    pub quantity: f64,
}

// 包件管理相关
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProductPackage {
    pub id: String,
    pub product_id: String,
    pub package_name: String,
    pub package_code: String,
    pub quantity: i32,
    pub is_shared: bool,
    pub shared_products: String,
    pub sort_order: i32,
    pub created_at: String,
}

#[derive(Debug, Deserialize)]
pub struct CreatePackageRequest {
    pub product_id: String,
    pub package_name: String,
    pub package_code: String,
    pub quantity: i32,
    pub is_shared: bool,
    pub shared_products: Vec<String>,
    pub sort_order: i32,
}

// 应收应付账单
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BillingRecord {
    pub id: String,
    pub party_type: String,
    pub party_id: String,
    pub party_name: String,
    pub order_id: String,
    pub order_no: String,
    pub amount: f64,
    pub paid_amount: f64,
    pub bill_type: String,
    pub bill_date: String,
    pub due_date: String,
    pub status: String,
    pub remark: String,
    pub created_at: String,
}

#[derive(Debug, Deserialize)]
pub struct CreateBillingRequest {
    pub party_type: String,
    pub party_id: String,
    pub party_name: String,
    pub order_id: String,
    pub order_no: String,
    pub amount: f64,
    pub bill_type: String,
    pub bill_date: String,
    pub due_date: String,
    pub remark: String,
}
