pub mod rest;
pub mod rest_article;
pub mod rest_embed;
pub mod rest_interact;
pub mod rest_member;
pub mod rest_message;
pub mod rest_room;
pub mod rest_user;
pub mod rest_voice;
pub mod token;

pub use channel::ChannelBus;

use actix_web::web::ServiceConfig;

pub fn configure(cfg: &mut ServiceConfig, bus: ChannelBus) {
    socketio::configure_at(cfg, "/socket.io", bus.io().clone());
    socketio::configure_at(cfg, "/socket.io/", bus.io().clone());
    cfg.service(
        actix_web::web::resource("/ping")
            .route(actix_web::web::get().to(rest::ping)),
    )
    .service(
        actix_web::web::resource("/csrf")
            .route(actix_web::web::get().to(rest::csrf_token)),
    );
    cfg.service(
        actix_web::web::resource("/rooms/{room_id}/messages")
            .route(actix_web::web::get().to(rest_message::list_messages))
            .route(actix_web::web::post().to(rest_message::create_message)),
    )
    .service(
        actix_web::web::resource("/rooms/{room_id}/messages/around")
            .route(actix_web::web::get().to(rest_message::messages_around)),
    )
    .service(
        actix_web::web::resource("/rooms/{room_id}/messages/missed")
            .route(actix_web::web::get().to(rest_message::missed_messages)),
    )
    .service(
        actix_web::web::resource("/messages/{message_id}")
            .route(actix_web::web::patch().to(rest_message::update_message))
            .route(actix_web::web::delete().to(rest_message::revoke_message)),
    )
    .service(
        actix_web::web::resource("/search")
            .route(actix_web::web::get().to(rest_message::search)),
    );
    cfg.service(
        actix_web::web::resource("/rooms")
            .route(actix_web::web::get().to(rest_room::list_rooms))
            .route(actix_web::web::post().to(rest_room::room_create)),
    )
    .service(
        actix_web::web::resource("/rooms/{room_id}")
            .route(actix_web::web::get().to(rest_room::room_get))
            .route(actix_web::web::patch().to(rest_room::room_update))
            .route(actix_web::web::delete().to(rest_room::room_delete)),
    )
    .service(
        actix_web::web::resource("/rooms/{room_id}/subscribe")
            .route(actix_web::web::post().to(rest_room::subscribe))
            .route(actix_web::web::delete().to(rest_room::unsubscribe)),
    )
    .service(
        actix_web::web::resource("/rooms/{room_id}/members")
            .route(actix_web::web::post().to(rest_room::access_grant)),
    )
    .service(
        actix_web::web::resource("/workspaces/{workspace_id}/members").route(
            actix_web::web::get().to(rest_member::list_workspace_members),
        ),
    )
    .service(
        actix_web::web::resource("/rooms/{room_id}/members/{user_id}")
            .route(actix_web::web::delete().to(rest_room::access_revoke)),
    )
    .service(
        actix_web::web::resource("/workspaces/{workspace_id}/categories")
            .route(actix_web::web::post().to(rest_room::category_create)),
    )
    .service(
        actix_web::web::resource("/categories/{category_id}")
            .route(actix_web::web::patch().to(rest_room::category_update))
            .route(actix_web::web::delete().to(rest_room::category_delete)),
    );
    cfg.service(
        actix_web::web::resource("/rooms/{room_id}/reactions")
            .route(actix_web::web::post().to(rest_interact::reaction_add))
            .route(actix_web::web::delete().to(rest_interact::reaction_remove)),
    )
    .service(
        actix_web::web::resource("/rooms/{room_id}/threads")
            .route(actix_web::web::post().to(rest_interact::thread_create))
            .route(actix_web::web::get().to(rest_interact::thread_list)),
    )
    .service(
        actix_web::web::resource("/threads/{thread_id}/resolve")
            .route(actix_web::web::patch().to(rest_interact::thread_resolve)),
    )
    .service(
        actix_web::web::resource("/threads/{thread_id}/archive")
            .route(actix_web::web::patch().to(rest_interact::thread_archive)),
    )
    .service(
        actix_web::web::resource("/rooms/{room_id}/pins")
            .route(actix_web::web::post().to(rest_interact::pin_add))
            .route(actix_web::web::delete().to(rest_interact::pin_remove)),
    )
    .service(
        actix_web::web::resource("/rooms/{room_id}/drafts")
            .route(actix_web::web::put().to(rest_interact::draft_save))
            .route(actix_web::web::delete().to(rest_interact::draft_clear)),
    )
    .service(
        actix_web::web::resource("/rooms/{room_id}/typing")
            .route(actix_web::web::post().to(rest_interact::typing)),
    );
    cfg.service(
        actix_web::web::resource("/rooms/{room_id}/read-receipt")
            .route(actix_web::web::post().to(rest_member::read_receipt)),
    )
    .service(
        actix_web::web::resource("/rooms/{room_id}/dnd")
            .route(actix_web::web::patch().to(rest_member::dnd_update)),
    )
    .service(
        actix_web::web::resource("/notifications/{id}/read").route(
            actix_web::web::patch().to(rest_member::notification_mark_read),
        ),
    )
    .service(actix_web::web::resource("/notifications/read-all").route(
        actix_web::web::post().to(rest_member::notification_mark_all_read),
    ))
    .service(
        actix_web::web::resource("/notifications/{id}").route(
            actix_web::web::delete().to(rest_member::notification_archive),
        ),
    )
    .service(
        actix_web::web::resource("/presence")
            .route(actix_web::web::post().to(rest_member::presence_update)),
    )
    .service(
        actix_web::web::resource("/custom-status").route(
            actix_web::web::post().to(rest_member::custom_status_update),
        ),
    )
    .service(
        actix_web::web::resource("/invites")
            .route(actix_web::web::post().to(rest_member::invite_create)),
    )
    .service(
        actix_web::web::resource("/invites/accept")
            .route(actix_web::web::post().to(rest_member::invite_accept)),
    )
    .service(
        actix_web::web::resource("/invites/{id}")
            .route(actix_web::web::delete().to(rest_member::invite_revoke)),
    )
    .service(
        actix_web::web::resource("/workspaces/{workspace_id}/bans")
            .route(actix_web::web::post().to(rest_member::ban_create)),
    )
    .service(
        actix_web::web::resource("/workspaces/{workspace_id}/bans/{user_id}")
            .route(actix_web::web::delete().to(rest_member::ban_remove)),
    );
    cfg.service(
        actix_web::web::resource("/rooms/{room_id}/voice/join")
            .route(actix_web::web::post().to(rest_voice::voice_join)),
    )
    .service(
        actix_web::web::resource("/rooms/{room_id}/voice/leave")
            .route(actix_web::web::post().to(rest_voice::voice_leave)),
    )
    .service(
        actix_web::web::resource("/rooms/{room_id}/voice/mute")
            .route(actix_web::web::post().to(rest_voice::voice_mute)),
    )
    .service(
        actix_web::web::resource("/rooms/{room_id}/voice/deaf")
            .route(actix_web::web::post().to(rest_voice::voice_deaf)),
    )
    .service(
        actix_web::web::resource("/rooms/{room_id}/screen-share")
            .route(actix_web::web::post().to(rest_voice::screen_share)),
    );
    cfg.service(
        actix_web::web::resource("/users/summary/{username}")
            .route(actix_web::web::get().to(rest_user::user_summary)),
    );
    // --- Article routes (waterfall post channels) ---
    cfg.service(
        actix_web::web::resource("/channels/{channel_id}/articles")
            .route(actix_web::web::post().to(rest_article::article_create))
            .route(actix_web::web::get().to(rest_article::article_list)),
    )
    .service(
        actix_web::web::resource("/channels/{channel_id}/articles/{article_id}")
            .route(actix_web::web::get().to(rest_article::article_get))
            .route(actix_web::web::patch().to(rest_article::article_update))
            .route(actix_web::web::delete().to(rest_article::article_delete)),
    )
    .service(
        actix_web::web::resource("/articles/{article_id}/like")
            .route(actix_web::web::post().to(rest_article::article_like)),
    )
    .service(
        actix_web::web::resource("/articles/{article_id}/comments")
            .route(actix_web::web::post().to(rest_article::article_comment_create))
            .route(actix_web::web::get().to(rest_article::article_comment_list)),
    )
    .service(
        actix_web::web::resource("/articles/{article_id}/comments/{comment_id}")
            .route(actix_web::web::delete().to(rest_article::article_comment_delete)),
    )
    .service(
        actix_web::web::resource("/articles/{article_id}/likes")
            .route(actix_web::web::get().to(rest_article::article_liked_users)),
    );
    cfg.service(
        actix_web::web::resource("/embed/twitter")
            .route(actix_web::web::get().to(rest_embed::twitter_oembed)),
    );
    cfg.service(
        actix_web::web::resource("/token")
            .route(actix_web::web::post().to(token::generate_token)),
    );
    cfg.app_data(actix_web::web::Data::new(bus));
}
