syntax = "proto3";

package userorder;

import "public_messages.proto";
import "google/protobuf/empty.proto";
import "google/protobuf/wrappers.proto";
import "google/protobuf/duration.proto";  
import "google/protobuf/timestamp.proto";
import "google/api/annotations.proto";

option csharp_namespace = "CMSMicroservice.Protobuf.Protos.UserOrder";

service UserOrderContract
{
    rpc CreateNewUserOrder(CreateNewUserOrderRequest) returns (CreateNewUserOrderResponse){
        option (google.api.http) = {
            post: "/CreateNewUserOrder"
            body: "*"
        };
    };
    rpc UpdateUserOrder(UpdateUserOrderRequest) returns (google.protobuf.Empty){
        option (google.api.http) = {
            put: "/UpdateUserOrder"
            body: "*"
        };
    };
    rpc DeleteUserOrder(DeleteUserOrderRequest) returns (google.protobuf.Empty){
        option (google.api.http) = {
            delete: "/DeleteUserOrder"
            body: "*"
        };
    };
    rpc GetUserOrder(GetUserOrderRequest) returns (GetUserOrderResponse){
        option (google.api.http) = {
            get: "/GetUserOrder"
            
        };
    };
    rpc GetAllUserOrderByFilter(GetAllUserOrderByFilterRequest) returns (GetAllUserOrderByFilterResponse){
        option (google.api.http) = {
            get: "/GetAllUserOrderByFilter"
            
        };
    };
    rpc SubmitShopBuyOrder(SubmitShopBuyOrderRequest) returns (SubmitShopBuyOrderResponse){
        option (google.api.http) = {
            post: "/SubmitShopBuyOrder"
            body: "*"
        };
    };
    rpc CancelOrder(CancelOrderRequest) returns (CancelOrderResponse){
        option (google.api.http) = {
            post: "/CancelOrder"
            body: "*"
        };
    };
    
    // Order Management
    rpc UpdateOrderStatus(UpdateOrderStatusRequest) returns (UpdateOrderStatusResponse){
        option (google.api.http) = {
            post: "/UpdateOrderStatus"
            body: "*"
        };
    };
    rpc GetOrdersByDateRange(GetOrdersByDateRangeRequest) returns (GetOrdersByDateRangeResponse){
        option (google.api.http) = {
            get: "/GetOrdersByDateRange"
        };
    };
    rpc ApplyDiscountToOrder(ApplyDiscountToOrderRequest) returns (ApplyDiscountToOrderResponse){
        option (google.api.http) = {
            post: "/ApplyDiscountToOrder"
            body: "*"
        };
    };
    rpc CalculateOrderPV(CalculateOrderPVRequest) returns (CalculateOrderPVResponse){
        option (google.api.http) = {
            get: "/CalculateOrderPV"
        };
    };
    
    // ============= Customer-specific Methods =============
    
    rpc CreateNewOrderForCustomer(CreateNewUserOrderRequest) returns (CreateNewUserOrderResponse){
        option (google.api.http) = {
            post: "/Customer/CreateOrder"
            body: "*"
        };
    };
    rpc SubmitOrderForCustomer(SubmitShopBuyOrderRequest) returns (SubmitShopBuyOrderResponse){
        option (google.api.http) = {
            post: "/Customer/SubmitOrder"
            body: "*"
        };
    };
    rpc GetCustomerOrders(GetAllUserOrderByFilterRequest) returns (GetAllUserOrderByFilterResponse){
        option (google.api.http) = {
            get: "/Customer/GetOrders"
            
        };
    };
    rpc GetCustomerOrder(GetUserOrderRequest) returns (GetUserOrderResponse){
        option (google.api.http) = {
            get: "/Customer/GetOrder"
            
        };
    };
    rpc CustomerCancelOrder(CustomerCancelOrderRequest) returns (CustomerCancelOrderResponse){
        option (google.api.http) = {
            post: "/Customer/CancelOrder"
            body: "*"
        };
    };
    rpc GetCustomerOrderHistory(GetCustomerOrderHistoryRequest) returns (GetCustomerOrderHistoryResponse){
        option (google.api.http) = {
            get: "/Customer/GetOrderHistory"
        };
    };
    rpc CustomerTrackOrder(CustomerTrackOrderRequest) returns (CustomerTrackOrderResponse){
        option (google.api.http) = {
            get: "/Customer/TrackOrder"
        };
    };
    rpc CustomerReorderPreviousOrder(CustomerReorderRequest) returns (CustomerReorderResponse){
        option (google.api.http) = {
            post: "/Customer/Reorder"
            body: "*"
        };
    };
    
    rpc GetVATRate(google.protobuf.Empty) returns (GetVATRateResponse){
        option (google.api.http) = {
            get: "/Customer/GetVATRate"
        };
    };
}
message CreateNewUserOrderRequest
{
    int64 amount = 1;
    int64 package_id = 2;
    google.protobuf.Int64Value transaction_id = 3;
    oneof PaymentStatus_item 
    {
        messages.PaymentStatus payment_status = 4;
    }
    google.protobuf.Timestamp payment_date = 5;
    int64 user_id = 6;
    int64 user_address_id = 7;
    oneof PaymentMethod_item 
    {
        messages.PaymentMethod payment_method = 8;
    }
}
message CreateNewUserOrderResponse
{
    int64 id = 1;
}
message UpdateUserOrderRequest
{
    int64 id = 1;
    google.protobuf.Int64Value amount = 2;
    google.protobuf.Int64Value package_id = 3;
    google.protobuf.Int64Value transaction_id = 4;
    oneof PaymentStatus_item 
    {
        messages.PaymentStatus payment_status = 5;
    }
    google.protobuf.Timestamp payment_date = 6;
    google.protobuf.Int64Value user_id = 7;
    google.protobuf.Int64Value user_address_id = 8;
    oneof PaymentMethod_item 
    {
        messages.PaymentMethod payment_method = 9;
    }
    // وضعیت ارسال و اطلاعات پستی
    oneof DeliveryStatus_item 
    {
        messages.DeliveryStatus delivery_status = 10;
    }
    google.protobuf.StringValue tracking_code = 11;
    google.protobuf.StringValue delivery_description = 12;
}
message DeleteUserOrderRequest
{
    int64 id = 1;
}
message GetUserOrderRequest
{
    int64 id = 1;
}
message GetUserOrderResponse
{
    int64 id = 1;
    int64 amount = 2;
    int64 package_id = 3;
    google.protobuf.Int64Value transaction_id = 4;
    oneof PaymentStatus_item 
    {
        messages.PaymentStatus payment_status = 5;
    }
    google.protobuf.Timestamp payment_date = 6;
    int64 user_id = 7;
    int64 user_address_id = 8;
    oneof PaymentMethod_item 
    {
        messages.PaymentMethod payment_method = 9;
    }
    google.protobuf.StringValue user_address_text = 10;
    repeated GetUserOrderResponseFactorDetail factor_details = 11;
    // وضعیت ارسال و اطلاعات پستی
    oneof DeliveryStatus_item 
    {
        messages.DeliveryStatus delivery_status = 12;
    }
    google.protobuf.StringValue tracking_code = 13;
    google.protobuf.StringValue delivery_description = 14;
    // نام کامل و کدملی کاربر
    google.protobuf.StringValue user_full_name = 15;
    google.protobuf.StringValue user_national_code = 16;
    // اطلاعات مالیات بر ارزش افزوده
    OrderVATInfo vat_info = 17;
    // اطلاعات پستی برای برچسب کارتن
    google.protobuf.StringValue postal_code = 18;
    google.protobuf.StringValue user_mobile = 19;
}

// اطلاعات مالیات بر ارزش افزوده
message OrderVATInfo
{
    double vat_rate = 1;          // نرخ مالیات (مثلاً 0.09)
    int64 base_amount = 2;        // مبلغ پایه (قبل از مالیات)
    int64 vat_amount = 3;         // مبلغ مالیات
    int64 total_amount = 4;       // مبلغ کل (پایه + مالیات)
    bool is_paid = 5;             // آیا پرداخت شده
}

message GetUserOrderResponseFactorDetail
{
    int64 product_id = 1;
    string product_title = 2;
    google.protobuf.StringValue product_thumbnail_path = 3;
    google.protobuf.Int64Value unit_price = 4;
    google.protobuf.Int32Value count = 5;
    google.protobuf.Int64Value unit_discount_price = 6;
}
message GetAllUserOrderByFilterRequest
{
    messages.PaginationState pagination_state = 1;
    google.protobuf.StringValue sort_by = 2;
    GetAllUserOrderByFilterFilter filter = 3;
}
message GetAllUserOrderByFilterFilter
{
    google.protobuf.Int64Value id = 1;
    google.protobuf.Int64Value amount = 2;
    google.protobuf.Int64Value package_id = 3;
    google.protobuf.Int64Value transaction_id = 4;
    oneof PaymentStatus_item 
    {
        messages.PaymentStatus payment_status = 5;
    }
    google.protobuf.Timestamp payment_date = 6;
    google.protobuf.Int64Value user_id = 7;
    google.protobuf.Int64Value user_address_id = 8;
    oneof PaymentMethod_item 
    {
        messages.PaymentMethod payment_method = 9;
    }
    // فیلتر وضعیت ارسال
    oneof DeliveryStatus_item 
    {
        messages.DeliveryStatus delivery_status = 10;
    }
    // فیلتر بازه تاریخ
    google.protobuf.Timestamp from_date = 11;
    google.protobuf.Timestamp to_date = 12;
}
message GetAllUserOrderByFilterResponse
{
    messages.MetaData meta_data = 1;
    repeated GetAllUserOrderByFilterResponseModel models = 2;
}
message GetAllUserOrderByFilterResponseModel
{
    int64 id = 1;
    int64 amount = 2;
    int64 package_id = 3;
    google.protobuf.Int64Value transaction_id = 4;
    oneof PaymentStatus_item 
    {
        messages.PaymentStatus payment_status = 5;
    }
    google.protobuf.Timestamp payment_date = 6;
    int64 user_id = 7;
    int64 user_address_id = 8;
    oneof PaymentMethod_item 
    {
        messages.PaymentMethod payment_method = 9;
    }
    google.protobuf.StringValue user_address_text = 10;
    repeated GetAllUserOrderByFilterResponseModelFactorDetail factor_details = 11;
    // وضعیت ارسال و اطلاعات پستی
    oneof DeliveryStatus_item 
    {
        messages.DeliveryStatus delivery_status = 12;
    }
    google.protobuf.StringValue tracking_code = 13;
    google.protobuf.StringValue delivery_description = 14;
    // نام کامل و کدملی کاربر
    google.protobuf.StringValue user_full_name = 15;
    google.protobuf.StringValue user_national_code = 16;
    // مبلغ و درصد مالیات بر ارزش افزوده
    int64 vat_amount = 17;
    double vat_percentage = 18;
}
message GetAllUserOrderByFilterResponseModelFactorDetail
{
    int64 product_id = 1;
    string product_title = 2;
    google.protobuf.StringValue product_thumbnail_path = 3;
    google.protobuf.Int64Value unit_price = 4;
    google.protobuf.Int32Value count = 5;
    google.protobuf.Int64Value unit_discount_price = 6;
}
message SubmitShopBuyOrderRequest
{
    int64 total_amount = 1;
    int64 user_id = 2;
}
message SubmitShopBuyOrderResponse
{
    int64 id = 1;
}

// CancelOrder Messages
message CancelOrderRequest
{
    int64 order_id = 1;
    string cancel_reason = 2;
    bool refund_payment = 3;
}

message CancelOrderResponse
{
    int64 order_id = 1;
    messages.DeliveryStatus status = 2;
    string message = 3;
    bool refund_processed = 4;
}

// Order Management Messages
message UpdateOrderStatusRequest
{
    int64 order_id = 1;
    int32 new_status = 2;
}

message UpdateOrderStatusResponse
{
    bool success = 1;
    string message = 2;
    int32 old_status = 3;
    int32 new_status = 4;
}

message GetOrdersByDateRangeRequest
{
    google.protobuf.Timestamp start_date = 1;
    google.protobuf.Timestamp end_date = 2;
    google.protobuf.Int32Value status = 3;
    google.protobuf.Int64Value user_id = 4;
    int32 page_number = 5;
    int32 page_size = 6;
}

message GetOrdersByDateRangeResponse
{
    messages.MetaData meta_data = 1;
    repeated OrderSummaryDto orders = 2;
}

message OrderSummaryDto
{
    int64 order_id = 1;
    string order_number = 2;
    int64 user_id = 3;
    string user_full_name = 4;
    int64 total_amount = 5;
    int32 status = 6;
    string status_name = 7;
    int32 items_count = 8;
    google.protobuf.Timestamp created_at = 9;
}

message ApplyDiscountToOrderRequest
{
    int64 order_id = 1;
    int64 discount_amount = 2;
    string reason = 3;
}

message ApplyDiscountToOrderResponse
{
    bool success = 1;
    string message = 2;
    int64 original_amount = 3;
    int64 discount_amount = 4;
    int64 final_amount = 5;
}

// ============= Customer Message Types =============

message CustomerCancelOrderRequest 
{
    int64 order_id = 1;
    string cancellation_reason = 2;
}

message CustomerCancelOrderResponse 
{
    bool success = 1;
    string message = 2;
    int64 refund_amount = 3;
    string refund_transaction_id = 4;
}

message GetCustomerOrderHistoryRequest 
{
    int64 user_id = 1;
    messages.PaginationState pagination_state = 2;
    OrderStatusEnum status_filter = 3;
    google.protobuf.Timestamp from_date = 4;
    google.protobuf.Timestamp to_date = 5;
}

message GetCustomerOrderHistoryResponse 
{
    messages.MetaData meta_data = 1;
    repeated CustomerOrderModel orders = 2;
}

message CustomerTrackOrderRequest 
{
    int64 order_id = 1;
}

message CustomerTrackOrderResponse 
{
    CustomerOrderModel order = 1;
    repeated OrderStatusHistory status_history = 2;
    DeliveryTrackingInfo delivery_info = 3;
}

message CustomerReorderRequest 
{
    int64 original_order_id = 1;
    bool use_current_prices = 2;
}

message CustomerReorderResponse 
{
    bool success = 1;
    string message = 2;
    int64 new_order_id = 3;
    int64 total_amount = 4;
}

message CustomerOrderModel 
{
    int64 id = 1;
    int64 amount = 2;
    int64 package_id = 3;
    string package_name = 4;
    OrderStatusEnum status = 5;
    string status_message = 6;
    google.protobuf.Timestamp order_date = 7;
    google.protobuf.Timestamp delivery_date = 8;
    string tracking_code = 9;
    int32 items_count = 10;
    bool can_cancel = 11;
    bool can_reorder = 12;
}

message OrderStatusHistory 
{
    OrderStatusEnum status = 1;
    string status_message = 2;
    google.protobuf.Timestamp changed_at = 3;
    string changed_by = 4;
}

message DeliveryTrackingInfo 
{
    string tracking_code = 1;
    string courier_name = 2;
    string estimated_delivery = 3;
    string current_location = 4;
    repeated DeliveryStep delivery_steps = 5;
}

message DeliveryStep 
{
    string step_name = 1;
    string step_description = 2;
    google.protobuf.Timestamp step_time = 3;
    bool is_completed = 4;
}

enum OrderStatusEnum 
{
    ORDER_STATUS_PENDING = 0;
    ORDER_STATUS_CONFIRMED = 1;
    ORDER_STATUS_PROCESSING = 2;
    ORDER_STATUS_SHIPPED = 3;
    ORDER_STATUS_DELIVERED = 4;
    ORDER_STATUS_CANCELLED = 5;
    ORDER_STATUS_REFUNDED = 6;
}

message CalculateOrderPVRequest
{
    int64 order_id = 1;
}

message CalculateOrderPVResponse
{
    int64 order_id = 1;
    int64 total_pv = 2;
    repeated ProductPVDto products = 3;
}

message ProductPVDto
{
    int64 product_id = 1;
    string product_title = 2;
    int32 quantity = 3;
    int64 unit_pv = 4;
    int64 total_pv = 5;
}

// GetVATRate - for frontend customer display
message GetVATRateResponse
{
    double vat_rate = 1;
    int32 vat_percentage = 2;
    bool is_enabled = 3;
}
