//! P1 (v1.0 protocol) legacy event handlers.
//!
//! P1 events use `event.type` for routing rather than the P2 `header.event_type`.
//! The dispatcher handles both protocols transparently.

use serde::{Deserialize, Serialize};

// ── P1 payload types ──────────────────────────────────────────────────────────

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct AppTicketEvent {
    #[serde(rename = "type", default)]
    pub event_type: String,
    #[serde(default)]
    pub app_id: String,
    #[serde(default)]
    pub app_ticket: String,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct P1OrderPaidV6 {
    #[serde(default)]
    pub app_id: String,
    #[serde(default)]
    pub order_id: String,
    #[serde(default)]
    pub price_plan_id: String,
    #[serde(default)]
    pub price_plan_type: String,
    #[serde(default)]
    pub seats: i32,
    #[serde(default)]
    pub buy_count: i32,
    #[serde(default)]
    pub create_time: String,
    #[serde(default)]
    pub pay_time: String,
    #[serde(default)]
    pub buy_type: String,
    #[serde(default)]
    pub src_order_id: String,
    #[serde(default)]
    pub operator_id: String,
    #[serde(default)]
    pub operator_name: String,
    #[serde(default)]
    pub tenant_key: String,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct P1AppUninstalledV6 {
    #[serde(default)]
    pub app_id: String,
    #[serde(default)]
    pub tenant_key: String,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct P1AppStatusChangedV6 {
    #[serde(default)]
    pub app_id: String,
    #[serde(default)]
    pub tenant_key: String,
    #[serde(default)]
    pub status: String,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct P1AppOpenV6 {
    #[serde(default)]
    pub app_id: String,
    #[serde(default)]
    pub tenant_key: String,
    #[serde(default)]
    pub applicants: Vec<serde_json::Value>,
    #[serde(default)]
    pub installer: serde_json::Value,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct P1MessageReceiveV1 {
    #[serde(rename = "type", default)]
    pub event_type: String,
    #[serde(default)]
    pub app_id: String,
    #[serde(default)]
    pub tenant_key: String,
    #[serde(default)]
    pub msg_type: String,
    #[serde(default)]
    pub message_id: String,
    #[serde(default)]
    pub root_id: String,
    #[serde(default)]
    pub parent_id: String,
    #[serde(default)]
    pub open_chat_id: String,
    #[serde(default)]
    pub chat_type: String,
    #[serde(default)]
    pub open_id: String,
    #[serde(default)]
    pub employee_id: String,
    #[serde(default)]
    pub union_id: String,
    #[serde(default)]
    pub open_message_id: String,
    #[serde(default)]
    pub text: String,
    #[serde(default)]
    pub text_without_at_bot: String,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct P1MessageReadV1 {
    #[serde(rename = "type", default)]
    pub event_type: String,
    #[serde(default)]
    pub app_id: String,
    #[serde(default)]
    pub tenant_key: String,
    #[serde(default)]
    pub open_id: String,
    #[serde(default)]
    pub open_message_ids: Vec<String>,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct P1AddBotV1 {
    #[serde(rename = "type", default)]
    pub event_type: String,
    #[serde(default)]
    pub app_id: String,
    #[serde(default)]
    pub tenant_key: String,
    #[serde(default)]
    pub chat_id: String,
    #[serde(default)]
    pub chat_name: String,
    #[serde(default)]
    pub chat_owner_id: String,
    #[serde(default)]
    pub chat_owner_open_id: String,
    #[serde(default)]
    pub operator_id: String,
    #[serde(default)]
    pub operator_open_id: String,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct P1RemoveBotV1 {
    #[serde(rename = "type", default)]
    pub event_type: String,
    #[serde(default)]
    pub app_id: String,
    #[serde(default)]
    pub tenant_key: String,
    #[serde(default)]
    pub chat_id: String,
    #[serde(default)]
    pub operator_id: String,
    #[serde(default)]
    pub operator_open_id: String,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct P1P2PChatCreatedV1 {
    #[serde(rename = "type", default)]
    pub event_type: String,
    #[serde(default)]
    pub app_id: String,
    #[serde(default)]
    pub tenant_key: String,
    #[serde(default)]
    pub chat_id: String,
    #[serde(default)]
    pub operator: serde_json::Value,
    #[serde(default)]
    pub user: serde_json::Value,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct P1ChatDisbandV1 {
    #[serde(rename = "type", default)]
    pub event_type: String,
    #[serde(default)]
    pub app_id: String,
    #[serde(default)]
    pub tenant_key: String,
    #[serde(default)]
    pub chat_id: String,
    #[serde(default)]
    pub operator_id: String,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct P1GroupSettingUpdatedV1 {
    #[serde(rename = "type", default)]
    pub event_type: String,
    #[serde(default)]
    pub app_id: String,
    #[serde(default)]
    pub tenant_key: String,
    #[serde(default)]
    pub chat_id: String,
    #[serde(default)]
    pub operator_id: String,
    #[serde(default)]
    pub after_change: serde_json::Value,
    #[serde(default)]
    pub before_change: serde_json::Value,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct P1UserInOutChatV1 {
    #[serde(rename = "type", default)]
    pub event_type: String,
    #[serde(default)]
    pub app_id: String,
    #[serde(default)]
    pub tenant_key: String,
    #[serde(default)]
    pub chat_id: String,
    #[serde(default)]
    pub operator_id: String,
    #[serde(default)]
    pub users: Vec<serde_json::Value>,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct P1UserStatusChangedV3 {
    #[serde(rename = "type", default)]
    pub event_type: String,
    #[serde(default)]
    pub app_id: String,
    #[serde(default)]
    pub tenant_key: String,
    #[serde(default)]
    pub open_id: String,
    #[serde(default)]
    pub employee_id: String,
    #[serde(default)]
    pub union_id: String,
    #[serde(default)]
    pub before_status: serde_json::Value,
    #[serde(default)]
    pub current_status: serde_json::Value,
    #[serde(default)]
    pub change_time: String,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct P1UserChangedV3 {
    #[serde(rename = "type", default)]
    pub event_type: String,
    #[serde(default)]
    pub app_id: String,
    #[serde(default)]
    pub tenant_key: String,
    #[serde(default)]
    pub open_id: String,
    #[serde(default)]
    pub employee_id: String,
    #[serde(default)]
    pub union_id: String,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct P1DepartmentChangedV3 {
    #[serde(rename = "type", default)]
    pub event_type: String,
    #[serde(default)]
    pub app_id: String,
    #[serde(default)]
    pub tenant_key: String,
    #[serde(default)]
    pub open_department_id: String,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct P1ContactScopeChangedV3 {
    #[serde(rename = "type", default)]
    pub event_type: String,
    #[serde(default)]
    pub app_id: String,
    #[serde(default)]
    pub tenant_key: String,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct P1ThirdPartyMeetingRoomChangedV1 {
    #[serde(rename = "type", default)]
    pub event_type: String,
    #[serde(default)]
    pub app_id: String,
    #[serde(default)]
    pub tenant_key: String,
    #[serde(default)]
    pub roomid: String,
    #[serde(default)]
    pub uid: String,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct P1LeaveApprovalV4 {
    #[serde(rename = "type", default)]
    pub event_type: String,
    #[serde(default)]
    pub app_id: String,
    #[serde(default)]
    pub tenant_key: String,
    #[serde(default)]
    pub open_id: String,
    #[serde(default)]
    pub employee_id: String,
    #[serde(default)]
    pub instance_code: String,
    #[serde(default)]
    pub start_time: String,
    #[serde(default)]
    pub end_time: String,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct P1WorkApprovalV4 {
    #[serde(rename = "type", default)]
    pub event_type: String,
    #[serde(default)]
    pub app_id: String,
    #[serde(default)]
    pub tenant_key: String,
    #[serde(default)]
    pub open_id: String,
    #[serde(default)]
    pub employee_id: String,
    #[serde(default)]
    pub instance_code: String,
    #[serde(default)]
    pub start_time: String,
    #[serde(default)]
    pub end_time: String,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct P1ShiftApprovalV4 {
    #[serde(rename = "type", default)]
    pub event_type: String,
    #[serde(default)]
    pub app_id: String,
    #[serde(default)]
    pub tenant_key: String,
    #[serde(default)]
    pub open_id: String,
    #[serde(default)]
    pub employee_id: String,
    #[serde(default)]
    pub instance_code: String,
    #[serde(default)]
    pub shift_time: String,
    #[serde(default)]
    pub new_shift_time: String,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct P1RemedyApprovalV4 {
    #[serde(rename = "type", default)]
    pub event_type: String,
    #[serde(default)]
    pub app_id: String,
    #[serde(default)]
    pub tenant_key: String,
    #[serde(default)]
    pub open_id: String,
    #[serde(default)]
    pub employee_id: String,
    #[serde(default)]
    pub instance_code: String,
    #[serde(default)]
    pub remedy_time: String,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct P1TripApprovalV4 {
    #[serde(rename = "type", default)]
    pub event_type: String,
    #[serde(default)]
    pub app_id: String,
    #[serde(default)]
    pub tenant_key: String,
    #[serde(default)]
    pub open_id: String,
    #[serde(default)]
    pub employee_id: String,
    #[serde(default)]
    pub instance_code: String,
    #[serde(default)]
    pub trips: Vec<serde_json::Value>,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct P1OutApprovalV4 {
    #[serde(rename = "type", default)]
    pub event_type: String,
    #[serde(default)]
    pub app_id: String,
    #[serde(default)]
    pub tenant_key: String,
    #[serde(default)]
    pub open_id: String,
    #[serde(default)]
    pub employee_id: String,
    #[serde(default)]
    pub instance_code: String,
    #[serde(default)]
    pub start_time: String,
    #[serde(default)]
    pub end_time: String,
}

// ── EventDispatcher P1 extension methods ──────────────────────────────────────

event_handlers! {
    on_p1_order_paid_v6 => P1OrderPaidV6
        : "order_paid",
    on_p1_app_uninstalled_v6 => P1AppUninstalledV6
        : "app_uninstalled",
    on_p1_app_status_changed_v6 => P1AppStatusChangedV6
        : "app_status_change",
    on_p1_app_open_v6 => P1AppOpenV6
        : "app_open",
    on_p1_message_receive_v1 => P1MessageReceiveV1
        : "message",
    on_p1_message_read_v1 => P1MessageReadV1
        : "message_read",
    on_p1_add_bot_v1 => P1AddBotV1
        : "add_bot",
    on_p1_remove_bot_v1 => P1RemoveBotV1
        : "remove_bot",
    on_p1_p2p_chat_created_v1 => P1P2PChatCreatedV1
        : "p2p_chat_create",
    on_p1_chat_disband_v1 => P1ChatDisbandV1
        : "chat_disband",
    on_p1_group_setting_updated_v1 => P1GroupSettingUpdatedV1
        : "group_setting_update",
    /// Registers a handler for user join/leave chat events (P1 protocol).
    /// Covers `add_user_to_chat`, `remove_user_from_chat`, and
    /// `revoke_add_user_from_chat` — all routed to the same handler.
    on_p1_user_in_out_chat_v1 => P1UserInOutChatV1
        : ["add_user_to_chat", "remove_user_from_chat", "revoke_add_user_from_chat"],
    on_p1_user_status_changed_v3 => P1UserStatusChangedV3
        : "user_status_change",
    /// Registers a handler for employee add/leave/update events (P1 protocol).
    /// Covers `user_add`, `user_leave`, and `user_update`.
    on_p1_user_changed_v3 => P1UserChangedV3
        : ["user_add", "user_leave", "user_update"],
    /// Registers a handler for department add/delete/update events (P1 protocol).
    /// Covers `dept_add`, `dept_delete`, and `dept_update`.
    on_p1_department_changed_v3 => P1DepartmentChangedV3
        : ["dept_add", "dept_delete", "dept_update"],
    on_p1_contact_scope_changed_v3 => P1ContactScopeChangedV3
        : "contact_scope_change",
    /// Registers a handler for third-party meeting room calendar events (P1 protocol).
    /// Covers created, updated, and deleted variants.
    on_p1_third_party_meeting_room_changed_v1 => P1ThirdPartyMeetingRoomChangedV1
        : ["third_party_meeting_room_event_created", "third_party_meeting_room_event_updated", "third_party_meeting_room_event_deleted"],
    on_p1_leave_approval_v4 => P1LeaveApprovalV4
        : "leave_approvalV2",
    on_p1_work_approval_v4 => P1WorkApprovalV4
        : "work_approval",
    on_p1_shift_approval_v4 => P1ShiftApprovalV4
        : "shift_approval",
    on_p1_remedy_approval_v4 => P1RemedyApprovalV4
        : "remedy_approval",
    on_p1_trip_approval_v4 => P1TripApprovalV4
        : "trip_approval",
    on_p1_out_approval_v4 => P1OutApprovalV4
        : "out_approval",
    on_app_ticket_event => AppTicketEvent
        : "app_ticket",
}
