public with sharing class SearchReportNameController {

    @AuraEnabled
    public static ResponseBodyLWC searchReportByName(String reportName) {
        String baseUrl = URL.getOrgDomainURL().toExternalForm();
        ResponseBodyLWC response = new ResponseBodyLWC();
        try {
            List<DataResponse> dataRespList = new List<DataResponse>();
            List<Id> reportIdList = new List<Id>();
            String sql = 'SELECT Id, Name, Description, OwnerId, CreatedById, FolderName, CreatedBy.Name, CreatedDate FROM Report WHERE ';
            List<RecentlyViewed> reportRecentlyList= [SELECT Id, Name FROM RecentlyViewed  WHERE Type ='Report'];
            for (RecentlyViewed reportRecently : reportRecentlyList) {
                reportIdList.add(reportRecently.Id);
            }
            if (String.isNotBlank(reportName)) {
                sql += ' Name like \'%' + reportName + '%\' ';
            } else {
                sql += 'Id IN :reportIdList ';
            }
            sql += ' LIMIT 200';
            System.debug('sql=====>' + sql);
            List<Report> reportList = DataBase.query(sql);
            for (Report re : reportList) {
                DataResponse dataRes = new DataResponse();
                dataRes.reportUrl = baseUrl + '/' + re.Id;
                dataRes.folderUrl = baseUrl + '/' + re.OwnerId;
                dataRes.createdbyUrl = baseUrl + '/' + re.CreatedById;
                dataRes.name = re.Name;
                dataRes.description = re.Description;
                dataRes.folderName = re.FolderName;
                dataRes.createdByName = re.CreatedBy.Name;
                dataRes.createdDate = re.CreatedDate;
                dataRespList.add(dataRes);
            }
            System.debug('dataRespList=====>' + dataRespList);
            response.entity = JSON.serialize(dataRespList);
            response.status = 'Success';
        } catch (Exception e) {
            response.status = 'fail';
            response.msg = e.getMessage() + '\n' + e.getLineNumber();
        }
        return response;
    }

    public class DataResponse {
        public String name {get; set;}
        public String reportUrl {get; set;}
        public String folderUrl {get; set;}
        public String createdbyUrl {get; set;}
        public String description {get; set;}
        public String folderName {get; set;}
        public String createdByName {get; set;}
        public DateTime createdDate {get; set;}

        public DataResponse() {

        }
    }
    
}