using System.Collections.Generic;
using System.IO;
using System.Linq;
using BrianMalingaPortfolio.ViewModels;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using System;
//the controller for all projects pages

namespace BrianMalingaPortfolio.Controllers
{
    public class ProjectsController : Controller
    {
        private readonly IWebHostEnvironment _environment;

        public ProjectsController(IWebHostEnvironment environment)
        {
            _environment = environment;
        }

        public IActionResult Portfolio()
        {
            var model = new PortfolioProjectsViewModel
            {
                Title = "Portfolio Websites",

                Category = "WEB DEVELOPMENT",

                Subtitle =
                    "A collection of portfolio websites created to showcase " +
                    "my development work, technical skills and professional journey.",

                GitHubUrl = "https://github.com/hulk26",

                Projects = GetPortfolioProjects("portfolio-websites")
            };

            return View(model);
        }


        private List<PortfolioProjectViewModel> GetPortfolioProjects(
    string folderName)
        {
            string folder = Path.Combine(
                _environment.WebRootPath,
                "downloads",
                "projects",
                folderName
            );

            if (!Directory.Exists(folder))
            {
                Directory.CreateDirectory(folder);

                return new List<PortfolioProjectViewModel>();
            }

            string[] allowedExtensions =
            {
        ".zip",
        ".rar",
        ".7z",
        ".pdf",
        ".doc",
        ".docx",
        ".txt",
        ".cs",
        ".cshtml",
        ".html",
        ".css",
        ".js",
        ".json"
    };

            return Directory
                .GetFiles(folder)
                .Where(file =>
                    allowedExtensions.Contains(
                        Path.GetExtension(file).ToLowerInvariant()
                    )
                )
                .Select(file =>
                {
                    string fileName = Path.GetFileName(file);

                    string extension = Path
                        .GetExtension(file)
                        .ToLowerInvariant();

                    return new PortfolioProjectViewModel
                    {
                        Name = FormatProjectName(
                            Path.GetFileNameWithoutExtension(file)
                        ),

                        Url = Url.Content(
                            $"~/downloads/projects/{folderName}/" +
                            Uri.EscapeDataString(fileName)
                        ),

                        Extension = extension
                            .TrimStart('.')
                            .ToUpperInvariant(),

                        FileType = GetPortfolioFileType(extension)
                    };
                })
                .OrderBy(project => project.Name)
                .ToList();
        }


        private static string GetPortfolioFileType(string extension)
        {
            return extension switch
            {
                ".zip" or ".rar" or ".7z" => "Portfolio Website",

                ".cs" => "C# Source File",

                ".cshtml" => "ASP.NET Core View",

                ".html" => "Website File",

                ".css" => "Stylesheet",

                ".js" => "JavaScript File",

                ".json" => "Project Data",

                ".pdf" => "Portfolio Documentation",

                ".doc" or ".docx" => "Project Document",

                ".txt" => "Project Notes",

                _ => "Portfolio Project"
            };
        }












        public IActionResult WebAppsAndSoftware()
        {
            var model = new WebAppsProjectsViewModel
            {
                Title = "Web Apps and Software",

                Category = "SOFTWARE DEVELOPMENT",

                Subtitle =
                    "A growing collection of web applications, software projects " +
                    "and practical development work created using modern technologies.",

                GitHubUrl = "https://github.com/hulk26",

                Projects = GetWebAppProjects("web-apps")
            };

            return View(model);
        }


        private List<WebAppProjectViewModel> GetWebAppProjects(string folderName)
        {
            string folder = Path.Combine(
                _environment.WebRootPath,
                "downloads",
                "projects",
                folderName
            );

            if (!Directory.Exists(folder))
            {
                Directory.CreateDirectory(folder);

                return new List<WebAppProjectViewModel>();
            }

            string[] allowedExtensions =
            {
        ".zip",
        ".rar",
        ".7z",
        ".pdf",
        ".doc",
        ".docx",
        ".txt",
        ".cs",
        ".cshtml",
        ".html",
        ".css",
        ".js",
        ".json",
        ".sql",
        ".exe"
    };

            return Directory
                .GetFiles(folder)
                .Where(file =>
                    allowedExtensions.Contains(
                        Path.GetExtension(file).ToLowerInvariant()
                    )
                )
                .Select(file =>
                {
                    string fileName = Path.GetFileName(file);

                    string extension = Path
                        .GetExtension(file)
                        .ToLowerInvariant();

                    return new WebAppProjectViewModel
                    {
                        Name = FormatProjectName(
                            Path.GetFileNameWithoutExtension(file)
                        ),

                        Url = Url.Content(
                            $"~/downloads/projects/{folderName}/" +
                            Uri.EscapeDataString(fileName)
                        ),

                        Extension = extension
                            .TrimStart('.')
                            .ToUpperInvariant(),

                        FileType = GetSoftwareFileType(extension)
                    };
                })
                .OrderBy(project => project.Name)
                .ToList();
        }


        private static string GetSoftwareFileType(string extension)
        {
            return extension switch
            {
                ".zip" or ".rar" or ".7z" => "Software Project",

                ".cs" => "C# Source File",

                ".cshtml" => "ASP.NET Core View",

                ".html" => "Web Application",

                ".css" => "Stylesheet",

                ".js" => "JavaScript Project",

                ".json" => "Project Data",

                ".sql" => "Database Script",

                ".pdf" => "Project Documentation",

                ".doc" or ".docx" => "Project Document",

                ".exe" => "Software Application",

                ".txt" => "Project Notes",

                _ => "Software Project"
            };
        }














        public IActionResult DataAnalytics()
        {
            var model = new DataAnalyticsProjectsViewModel
            {
                Title = "Data Analytics Projects",

                Category = "DATA ANALYTICS",

                Subtitle =
                    "A growing collection of practical dashboards, reports, " +
                    "spreadsheets and business intelligence projects.",

                GitHubUrl = "https://github.com/hulk26",

                Projects = GetDataAnalyticsProjects("data-analysis")
            };

            return View(model);
        }


        private List<DataAnalyticsProjectViewModel> GetDataAnalyticsProjects(
    string folderName)
        {
            var projects = new List<DataAnalyticsProjectViewModel>();

            string folder = Path.Combine(
                _environment.WebRootPath,
                "downloads",
                "projects",
                folderName
            );

            if (!Directory.Exists(folder))
            {
                Directory.CreateDirectory(folder);

                return projects;
            }

            string[] allowedExtensions =
            {
        ".pdf",
        ".pbix",
        ".xlsx",
        ".xls",
        ".csv",
        ".sql",
        ".zip",
        ".doc",
        ".docx",
        ".ppt",
        ".pptx"
    };

            return Directory
                .GetFiles(folder)
                .Where(file =>
                    allowedExtensions.Contains(
                        Path.GetExtension(file).ToLowerInvariant()
                    )
                )
                .Select(file =>
                {
                    string fileName = Path.GetFileName(file);

                    string extension = Path
                        .GetExtension(file)
                        .ToLowerInvariant();

                    return new DataAnalyticsProjectViewModel
                    {
                        Name = FormatProjectName(
                            Path.GetFileNameWithoutExtension(file)
                        ),

                        Url = Url.Content(
                            $"~/downloads/projects/{folderName}/" +
                            Uri.EscapeDataString(fileName)
                        ),

                        Extension = extension
                            .TrimStart('.')
                            .ToUpperInvariant(),

                        FileType = GetProjectFileType(extension)
                    };
                })
                .OrderBy(project => project.Name)
                .ToList();
        }

        private static string FormatProjectName(string fileName)
        {
            return fileName
                .Replace("-", " ")
                .Replace("_", " ")
                .Trim();
        }

        private static string GetProjectFileType(string extension)
        {
            return extension switch
            {
                ".pbix" => "Power BI Project",

                ".xlsx" or ".xls" => "Excel Project",

                ".csv" => "Data File",

                ".sql" => "SQL Project",

                ".pdf" => "Project Report",

                ".ppt" or ".pptx" => "Presentation",

                ".doc" or ".docx" => "Project Document",

                ".zip" => "Project Package",

                _ => "Data Analytics Project"
            };
        }

    }

}