pub mod activity;
pub mod audit;
pub mod billing;
pub mod board;
pub mod info;
pub mod init;
pub mod invitation;
pub mod join_answers;
pub mod join_request;
pub mod join_settings;
pub mod labels;
pub mod like;
pub mod members;
pub mod repo;
pub mod settings;
pub mod transfer_repo;
pub mod watch;

use actix_web::web;

#[derive(serde::Deserialize, utoipa::IntoParams)]
pub struct PageQuery {
    pub page: Option<u64>,
    pub per_page: Option<u64>,
}

#[derive(serde::Deserialize, utoipa::IntoParams)]
pub struct RepoPagerQuery {
    pub limit: Option<u64>,
    pub cursor: Option<String>,
}

#[derive(serde::Deserialize, utoipa::IntoParams)]
pub struct UserPagerQuery {
    pub page: Option<i64>,
    pub par_page: Option<i64>,
}

impl From<UserPagerQuery> for service::Pager {
    fn from(q: UserPagerQuery) -> Self {
        service::Pager {
            page: q.page.unwrap_or(1),
            par_page: q.par_page.unwrap_or(20),
        }
    }
}

#[derive(serde::Deserialize, utoipa::IntoParams)]
pub struct JoinRequestQuery {
    pub status: Option<String>,
    pub page: Option<u64>,
    pub per_page: Option<u64>,
}

pub fn init_project_routes(cfg: &mut web::ServiceConfig) {
    cfg.service(
        web::scope("/projects")
            .route("", web::post().to(init::project_create))
            .route(
                "/me/invitations",
                web::get().to(invitation::project_my_invitations),
            )
            .route(
                "/me/join-requests",
                web::get().to(join_request::project_my_join_requests),
            )
            .route("/{project_name}", web::get().to(info::project_info))
            .route("/{project_name}/repos", web::get().to(repo::project_repos))
            .route(
                "/{project_name}/repos",
                web::post().to(repo::project_repo_create),
            )
            .route(
                "/{project_name}/members",
                web::get().to(members::project_members),
            )
            .route(
                "/{project_name}/members/role",
                web::patch().to(members::project_update_member_role),
            )
            .route(
                "/{project_name}/members/{user_id}",
                web::delete().to(members::project_remove_member),
            )
            .route(
                "/{project_name}/labels",
                web::get().to(labels::project_labels),
            )
            .route(
                "/{project_name}/labels",
                web::post().to(labels::project_create_label),
            )
            .route(
                "/{project_name}/labels/{label_id}",
                web::get().to(labels::project_get_label),
            )
            .route(
                "/{project_name}/labels/{label_id}",
                web::patch().to(labels::project_update_label),
            )
            .route(
                "/{project_name}/labels/{label_id}",
                web::delete().to(labels::project_delete_label),
            )
            .route("/{project_name}/like", web::post().to(like::project_like))
            .route(
                "/{project_name}/like",
                web::delete().to(like::project_unlike),
            )
            .route("/{project_name}/like", web::get().to(like::project_is_like))
            .route(
                "/{project_name}/likes/count",
                web::get().to(like::project_likes_count),
            )
            .route(
                "/{project_name}/likes/users",
                web::get().to(like::project_like_users),
            )
            .route(
                "/{project_name}/watch",
                web::post().to(watch::project_watch),
            )
            .route(
                "/{project_name}/watch",
                web::delete().to(watch::project_unwatch),
            )
            .route(
                "/{project_name}/watch",
                web::get().to(watch::project_is_watch),
            )
            .route(
                "/{project_name}/watches/count",
                web::get().to(watch::project_watches_count),
            )
            .route(
                "/{project_name}/watches/users",
                web::get().to(watch::project_watch_users),
            )
            .route(
                "/{project_name}/settings/name",
                web::patch().to(settings::project_exchange_name),
            )
            .route(
                "/{project_name}/settings/visibility",
                web::patch().to(settings::project_exchange_visibility),
            )
            .route(
                "/{project_name}/settings/title",
                web::patch().to(settings::project_exchange_title),
            )
            .route(
                "/{project_name}/audit-logs",
                web::get().to(audit::project_audit_logs),
            )
            .route(
                "/{project_name}/audit-logs/{log_id}",
                web::get().to(audit::project_audit_log),
            )
            .route(
                "/{project_name}/audit-logs",
                web::post().to(audit::project_log_audit),
            )
            .route(
                "/{project_name}/activities",
                web::get().to(activity::project_activities),
            )
            .route(
                "/{project_name}/activities",
                web::post().to(activity::project_log_activity),
            )
            .route(
                "/{project_name}/billing",
                web::get().to(billing::project_billing),
            )
            .route(
                "/{project_name}/billing/history",
                web::get().to(billing::project_billing_history),
            )
            .route(
                "/{project_name}/invitations",
                web::get().to(invitation::project_invitations),
            )
            .route(
                "/{project_name}/invitations",
                web::post().to(invitation::project_invite_user),
            )
            .route(
                "/{project_name}/invitations/accept",
                web::post().to(invitation::project_accept_invitation),
            )
            .route(
                "/{project_name}/invitations/reject",
                web::post().to(invitation::project_reject_invitation),
            )
            .route(
                "/{project_name}/invitations/{user_id}",
                web::delete().to(invitation::project_cancel_invitation),
            )
            .route(
                "/{project_name}/join-settings",
                web::get().to(join_settings::project_join_settings),
            )
            .route(
                "/{project_name}/join-settings",
                web::patch().to(join_settings::project_update_join_settings),
            )
            .route(
                "/{project_name}/join-requests",
                web::get().to(join_request::project_join_requests),
            )
            .route(
                "/{project_name}/join-requests",
                web::post().to(join_request::project_submit_join_request),
            )
            .route(
                "/{project_name}/join-requests/{request_id}",
                web::patch().to(join_request::project_process_join_request),
            )
            .route(
                "/{project_name}/join-requests/{request_id}",
                web::delete().to(join_request::project_cancel_join_request),
            )
            .route(
                "/{project_name}/join-requests/{request_id}/answers",
                web::get().to(join_answers::project_join_answers),
            )
            .route(
                "/{project_name}/join-requests/{request_id}/answers",
                web::post().to(join_answers::project_submit_join_answers),
            )
            .route(
                "/{source_project}/repos/{repo_name}/transfer",
                web::post().to(transfer_repo::project_transfer_repo),
            )
            // Boards
            .route("/{project_name}/boards", web::get().to(board::board_list))
            .route(
                "/{project_name}/boards",
                web::post().to(board::board_create),
            )
            .route(
                "/{project_name}/boards/{board_id}",
                web::get().to(board::board_get),
            )
            .route(
                "/{project_name}/boards/{board_id}",
                web::patch().to(board::board_update),
            )
            .route(
                "/{project_name}/boards/{board_id}",
                web::delete().to(board::board_delete),
            )
            // Columns
            .route(
                "/{project_name}/boards/{board_id}/columns",
                web::post().to(board::column_create),
            )
            .route(
                "/{project_name}/columns/{column_id}",
                web::patch().to(board::column_update),
            )
            .route(
                "/{project_name}/columns/{column_id}",
                web::delete().to(board::column_delete),
            )
            // Cards
            .route("/{project_name}/cards", web::post().to(board::card_create))
            .route(
                "/{project_name}/cards/{card_id}",
                web::patch().to(board::card_update),
            )
            .route(
                "/{project_name}/cards/{card_id}/move",
                web::post().to(board::card_move),
            )
            .route(
                "/{project_name}/cards/{card_id}",
                web::delete().to(board::card_delete),
            ),
    );
}
