-module(admin_handler_reports). -behaviour(cowboy_handler). -export([init/2]). -export([trails/0]). -include("records.hrl"). -spec init(cowboy_req:req(), any()) -> {ok, cowboy_req:req(), any()}. init(Req, _Opts) -> case cowboy_req:method(Req) of <<"GET">> -> list_reports(Req); _ -> handler_utils:send_error(Req, 405, <<"Method not allowed">>) end. -spec trails() -> [map()]. trails() -> [ #{ path => <<"/v1/admin/reports">>, method => <<"GET">>, description => <<"List all reports (admin)">>, tags => [<<"Reports">>], parameters => [ #{name => <<"status">>, in => <<"query">>, schema => #{type => string, enum => [<<"pending">>, <<"reviewed">>, <<"dismissed">>]}, description => <<"Filter by status">>}, #{name => <<"target_type">>, in => <<"query">>, schema => #{type => string, enum => [<<"calendar">>, <<"event">>, <<"review">>]}, description => <<"Filter by target type">>}, #{name => <<"q">>, in => <<"query">>, schema => #{type => string}, description => <<"Search in reason">>}, #{name => <<"sort">>, in => <<"query">>, schema => #{type => string, enum => [<<"created_at">>, <<"status">>]}, description => <<"Sort field">>}, #{name => <<"order">>, in => <<"query">>, schema => #{type => string, enum => [<<"asc">>, <<"desc">>]}, description => <<"Sort direction">>}, #{name => <<"limit">>, in => <<"query">>, schema => #{type => integer}, description => <<"Page size">>}, #{name => <<"offset">>, in => <<"query">>, schema => #{type => integer}, description => <<"Offset">>} ], responses => #{ 200 => #{ description => <<"Array of reports">>, content => #{<<"application/json">> => #{schema => #{ type => array, items => report_schema() }}} } } } ]. report_schema() -> #{ type => object, properties => #{ id => #{type => string}, reporter_id => #{type => string}, target_type => #{type => string, enum => [<<"calendar">>, <<"event">>, <<"review">>]}, target_id => #{type => string}, reason => #{type => string}, status => #{type => string, enum => [<<"pending">>, <<"reviewed">>, <<"dismissed">>]}, created_at => #{type => string, format => <<"date-time">>}, resolved_at => #{type => string, format => <<"date-time">>, nullable => true}, resolved_by => #{type => string, nullable => true} } }. %%% Internal functions -spec list_reports(cowboy_req:req()) -> {ok, binary(), cowboy_req:req()}. list_reports(Req) -> case handler_utils:auth_admin(Req) of {ok, AdminId, Req1} -> Filters = parse_report_filters(Req1), Pagination0 = handler_utils:parse_pagination_params(Req1), Pagination = Pagination0#{ sort => maps:get(sort, Pagination0, <<"created_at">>), order => maps:get(order, Pagination0, <<"desc">>) }, case logic_report:list_reports(AdminId) of {ok, AllReports} -> Filtered = apply_report_filters(AllReports, Filters), Sorted = sort_reports(Filtered, Pagination), Total = length(Sorted), Page = lists:sublist(Sorted, maps:get(offset, Pagination) + 1, maps:get(limit, Pagination)), Json = [handler_utils:report_to_json(R) || R <- Page], ExtraHeaders = handler_utils:pagination_headers(Pagination, Total), handler_utils:send_json(Req1, 200, Json, ExtraHeaders); {error, access_denied} -> handler_utils:send_error(Req1, 403, <<"Admin access required">>) end; {error, Code, Msg, Req1} -> handler_utils:send_error(Req1, Code, Msg) end. parse_report_filters(Req) -> Qs = cowboy_req:parse_qs(Req), StatusBin = proplists:get_value(<<"status">>, Qs), TargetBin = proplists:get_value(<<"target_type">>, Qs), #{ status => convert_to_atom(StatusBin), target_type => convert_to_atom(TargetBin), q => proplists:get_value(<<"q">>, Qs) }. convert_to_atom(undefined) -> undefined; convert_to_atom(Bin) when is_binary(Bin) -> try binary_to_existing_atom(Bin, utf8) catch error:badarg -> Bin end. apply_report_filters(Reports, Filters) -> Status = maps:get(status, Filters, undefined), TargetType = maps:get(target_type, Filters, undefined), Q = maps:get(q, Filters, undefined), F1 = case Status of undefined -> Reports; _ -> [R || R <- Reports, R#report.status =:= Status] end, F2 = case TargetType of undefined -> F1; _ -> [R || R <- F1, R#report.target_type =:= TargetType] end, case Q of undefined -> F2; _ -> [R || R <- F2, string:str(binary_to_list(R#report.reason), binary_to_list(Q)) > 0] end. sort_reports(Reports, #{sort := Sort, order := Order}) -> Field = binary_to_existing_atom(Sort, utf8), lists:sort( fun(A, B) -> ValA = report_field(A, Field), ValB = report_field(B, Field), if Order == <<"asc">> -> ValA =< ValB; true -> ValA >= ValB end end, Reports). report_field(#report{created_at = V}, created_at) -> V; report_field(#report{status = V}, status) -> V; report_field(_, _) -> undefined.