using Azure.Identity;
using Microsoft.Graph;
using Microsoft.Graph.Models;
using Microsoft.Kiota.Abstractions;
using System.IO.Pipes;
using System.Runtime.CompilerServices;
using System.Text;
using System.Web;

namespace ManagementApp.Models.Services
{
    public class Microsoft365Service
    {

        public int Status { set; get; } = -1;

        private GraphServiceClient graphClient;
        private string tenantID;
        private string clientID;
        private string[] scope;
        private string clientSecret;
        private string secretID;
        private Date secretExpiresDate;
        public string Host { set; get; }
        public Microsoft365Service() {
            this.clientID = "10012dab-5561-4cef-9b7a-a7f2c0bea704";
            this.tenantID = "8c8a0bb8-9582-4322-b9fc-11d031e5702a";
            this.scope = new[] { "https://graph.microsoft.com/.default" };
            this.clientSecret = "RRi8Q~CvY1GmY8~zKDGv6bdeBSyVvAHy4nOcCcKd";
            this.secretID = "d1b4e1a0-faae-42c1-b5e4-c4a26a345c5c";
            this.secretExpiresDate = new Date(2025, 8, 12);
            this.Host = string.Empty;

        }

        public string Message { get; set; } = "";



        public async Task<UserCollectionResponse?> GetUser()
        {
            var users = await graphClient.Users.GetAsync(u => { u.QueryParameters.Select = ["displayName", "jobTitle"]; });
            return users;
        }
        public async Task<SiteCollectionResponse?> GetDrives()
        {
            var sites = await graphClient.Sites.GetAsync();
            var siteInfor = (sites != null) ? sites.Value.Where(a => a.IsPersonalSite == false && a.DisplayName == "Storage Backup").FirstOrDefault() : null;
         

            //graphClient.Sites[""].Drives.GetAsync()
            return sites;
        }

        public async Task<string> ConnectMicrosoft365()
        {
            await Task.Run(() =>
            {
                var options = new ClientSecretCredentialOptions
                {
                    AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
                };

                var clientSecretCredential = new ClientSecretCredential(
                    this.tenantID, this.clientID, clientSecret, options);

                this.graphClient = new GraphServiceClient(clientSecretCredential, this.scope);
                this.Status = 0;
            });
            return "OK";
        }

    }
}
 