syntax = "proto3";

package discountorder;

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

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

service DiscountOrderContract
{
    rpc PlaceOrder(PlaceOrderRequest) returns (PlaceOrderResponse){
        option (google.api.http) = {
            post: "/PlaceOrder"
            body: "*"
        };
    };
    rpc CompleteOrderPayment(CompleteOrderPaymentRequest) returns (CompleteOrderPaymentResponse){
        option (google.api.http) = {
            post: "/CompleteOrderPayment"
            body: "*"
        };
    };
    rpc UpdateOrderStatus(UpdateOrderStatusRequest) returns (UpdateOrderStatusResponse){
        option (google.api.http) = {
            put: "/UpdateOrderStatus"
            body: "*"
        };
    };
    rpc GetOrderById(GetOrderByIdRequest) returns (GetOrderByIdResponse){
        option (google.api.http) = {
            get: "/GetOrderById"
        };
    };
    rpc GetUserOrders(GetUserOrdersRequest) returns (GetUserOrdersResponse){
        option (google.api.http) = {
            get: "/GetUserOrders"
        };
    };
    
    // Admin: Get All Discount Orders with filters
    rpc GetAllDiscountOrders(GetAllDiscountOrdersRequest) returns (GetAllDiscountOrdersResponse){
        option (google.api.http) = {
            get: "/GetAllDiscountOrders"
        };
    };
    
    // Admin: Get Sales Report
    rpc GetDiscountSalesReport(GetDiscountSalesReportRequest) returns (GetDiscountSalesReportResponse){
        option (google.api.http) = {
            get: "/GetDiscountSalesReport"
        };
    };

    // Customer: Verify discount order payment after gateway callback
    rpc CustomerVerifyDiscountOrderPayment(CustomerVerifyDiscountOrderPaymentRequest) returns (CustomerVerifyDiscountOrderPaymentResponse){
        option (google.api.http) = {
            post: "/CustomerVerifyDiscountOrderPayment"
            body: "*"
        };
    };
}

// Place Order (Initial Step - Create Order)
message PlaceOrderRequest
{
    int64 user_id = 1;
    int64 user_address_id = 2;
    int64 discount_balance_to_use = 3; // Amount from DiscountBalance wallet
    google.protobuf.StringValue notes = 4;
}

message PlaceOrderResponse
{
    bool success = 1;
    string message = 2;
    int64 order_id = 3;
    int64 gateway_amount = 4; // Amount to pay via gateway (if any)
    google.protobuf.StringValue payment_url = 5; // Payment gateway URL (if needed)
}

// Complete Order Payment (After Gateway Callback)
message CompleteOrderPaymentRequest
{
    int64 order_id = 1;
    google.protobuf.StringValue transaction_id = 2;
    bool payment_success = 3;
}

message CompleteOrderPaymentResponse
{
    bool success = 1;
    string message = 2;
}

// Update Order Status (Admin)
message UpdateOrderStatusRequest
{
    int64 order_id = 1;
    DeliveryStatus delivery_status = 2;
    google.protobuf.StringValue tracking_code = 3;
    google.protobuf.StringValue admin_notes = 4;
}

message UpdateOrderStatusResponse
{
    bool success = 1;
    string message = 2;
}

enum DeliveryStatus
{
    DELIVERY_PENDING = 0;
    DELIVERY_PROCESSING = 1;
    DELIVERY_SHIPPED = 2;
    DELIVERY_DELIVERED = 3;
    DELIVERY_CANCELLED = 4;
    DELIVERY_READY_FOR_OFFICE = 5;
    DELIVERY_RETURNED = 6;
}

// Get Order By Id
message GetOrderByIdRequest
{
    int64 order_id = 1;
    int64 user_id = 2; // For authorization check
}

message GetOrderByIdResponse
{
    int64 id = 1;
    int64 user_id = 2;
    string order_number = 3;
    AddressInfo address = 4;
    int64 total_price = 5;
    int64 discount_balance_used = 6;
    int64 gateway_amount = 7;
    bool payment_completed = 8;
    google.protobuf.StringValue transaction_id = 9;
    DeliveryStatus delivery_status = 10;
    google.protobuf.StringValue tracking_code = 11;
    google.protobuf.StringValue notes = 12;
    google.protobuf.StringValue admin_notes = 13;
    repeated OrderItemDto items = 14;
    google.protobuf.Timestamp created = 15;
    google.protobuf.Timestamp last_modified = 16;
    PaymentStatus payment_status = 17;
}

message AddressInfo
{
    int64 id = 1;
    string title = 2;
    string address = 3;
    string postal_code = 4;
    google.protobuf.StringValue phone = 5;
}

message OrderItemDto
{
    int64 product_id = 1;
    string product_title = 2;
    int64 unit_price = 3;
    int32 max_discount_percent = 4;
    int32 count = 5;
    int64 total_price = 6;
    int64 discount_amount = 7;
    int64 final_price = 8;
    // آدرس تصویر محصول
    string image_path = 9;
    string thumbnail_path = 10;
}

// Get User Orders
message GetUserOrdersRequest
{
    int64 user_id = 1;
    google.protobuf.BoolValue payment_completed = 2;
    google.protobuf.Int32Value delivery_status = 3; // DeliveryStatus as int
    int32 page_number = 4;
    int32 page_size = 5;
}

message GetUserOrdersResponse
{
    messages.MetaData meta_data = 1;
    repeated OrderSummaryDto models = 2;
}

message OrderSummaryDto
{
    int64 id = 1;
    string order_number = 2;
    int64 total_price = 3;
    int64 discount_balance_used = 4;
    int64 gateway_amount = 5;
    bool payment_completed = 6;
    DeliveryStatus delivery_status = 7;
    google.protobuf.StringValue tracking_code = 8;
    int32 items_count = 9;
    google.protobuf.Timestamp created = 10;
    PaymentStatus payment_status = 11;
}

// ===== Admin: Get All Discount Orders =====

message GetAllDiscountOrdersRequest
{
    google.protobuf.Int64Value user_id = 1;
    google.protobuf.Int32Value payment_status = 2;
    google.protobuf.Int32Value delivery_status = 3;
    google.protobuf.StringValue user_mobile = 4;
    google.protobuf.StringValue tracking_code = 5;
    google.protobuf.Timestamp from_date = 6;
    google.protobuf.Timestamp to_date = 7;
    google.protobuf.Int64Value min_amount = 8;
    google.protobuf.Int64Value max_amount = 9;
    int32 page_number = 10;
    int32 page_size = 11;
}

message GetAllDiscountOrdersResponse
{
    messages.MetaData meta_data = 1;
    repeated AdminOrderDto models = 2;
}

message AdminOrderDto
{
    int64 id = 1;
    int64 user_id = 2;
    string user_full_name = 3;
    string user_mobile = 4;
    int64 total_amount = 5;
    int64 discount_balance_used = 6;
    int64 gateway_amount_paid = 7;
    int64 vat_amount = 8;
    PaymentStatus payment_status = 9;
    google.protobuf.Timestamp payment_date = 10;
    DeliveryStatus delivery_status = 11;
    google.protobuf.Timestamp delivery_date = 12;
    google.protobuf.StringValue shipping_address = 13;
    google.protobuf.StringValue receiver_name = 14;
    google.protobuf.StringValue receiver_mobile = 15;
    google.protobuf.StringValue tracking_code = 16;
    google.protobuf.StringValue admin_note = 17;
    google.protobuf.Timestamp created = 18;
    google.protobuf.Timestamp last_modified = 19;
    int32 items_count = 20;
}

enum PaymentStatus
{
    PAYMENT_PENDING = 0;
    PAYMENT_COMPLETED = 1;
    PAYMENT_FAILED = 2;
    PAYMENT_REFUNDED = 3;
}

// ===== Sales Report Messages =====

enum SalesReportType
{
    REPORT_SUMMARY = 0;
    REPORT_DAILY = 1;
    REPORT_WEEKLY = 2;
    REPORT_MONTHLY = 3;
}

message GetDiscountSalesReportRequest
{
    google.protobuf.Timestamp from_date = 1;
    google.protobuf.Timestamp to_date = 2;
    SalesReportType report_type = 3;
}

message GetDiscountSalesReportResponse
{
    SalesSummary summary = 1;
    repeated SalesPeriodDto periods = 2;
    repeated TopSellingProductDto top_products = 3;
}

message SalesSummary
{
    int32 total_orders = 1;
    int32 completed_orders = 2;
    int32 pending_orders = 3;
    int32 cancelled_orders = 4;
    int64 total_sales_amount = 5;
    int64 total_discount_used = 6;
    int64 total_gateway_paid = 7;
    int64 total_vat_amount = 8;
    int64 average_order_value = 9;
    int32 unique_customers = 10;
    int32 total_products_sold = 11;
}

message SalesPeriodDto
{
    string period_label = 1;
    google.protobuf.Timestamp period_start = 2;
    google.protobuf.Timestamp period_end = 3;
    int32 orders_count = 4;
    int64 total_amount = 5;
    int64 discount_used = 6;
    int64 gateway_paid = 7;
}

message TopSellingProductDto
{
    int64 product_id = 1;
    string product_title = 2;
    google.protobuf.StringValue image_path = 3;
    int32 quantity_sold = 4;
    int64 total_revenue = 5;
    int32 orders_count = 6;
}

// ===== Customer Verify Discount Order Payment =====

message CustomerVerifyDiscountOrderPaymentRequest
{
    int64 order_id = 1;
    string authority = 2;
    string status = 3;
}

message CustomerVerifyDiscountOrderPaymentResponse
{
    bool success = 1;
    string message = 2;
    int64 order_id = 3;
}
