@using Radzen.Blazor.Spreadsheet
@using Radzen.Documents.Spreadsheet

<RadzenSpreadsheet Workbook=@workbook style="height: 600px" />

@code {
    Workbook workbook = new Workbook();

    protected override void OnInitialized()
    {
        BuildBudgetTracker();
        BuildTimesheet();
    }

    void BuildBudgetTracker()
    {
        var sheet = workbook.AddSheet("Budget Tracker", 30, 16);

        sheet.BeginUpdate();

        var titleFormat = new Format { Bold = true, FontSize = 14, BackgroundColor = "#1565c0", Color = "#ffffff", TextAlign = TextAlign.Center };
        var headerFormat = new Format { Bold = true, BackgroundColor = "#e3f2fd", Color = "#005da1", TextAlign = TextAlign.Center };
        var categoryFormat = new Format { Bold = true, BackgroundColor = "#f5f5f5", Color = "#212121" };
        var currencyFormat = new Format { NumberFormat = "$#,##0.00" };
        var totalFormat = new Format { Bold = true, NumberFormat = "$#,##0.00", BackgroundColor = "#e8f5e9", Color = "#0f2e10" };
        var overBudgetFormat = new Format { Bold = true, NumberFormat = "$#,##0.00", Color = "#c62828", BackgroundColor = "#ffebee" };

        sheet.Columns[0] = 140;
        for (int i = 1; i <= 13; i++) sheet.Columns[i] = 90;
        sheet.Columns[13] = 100;
        sheet.Columns[14] = 100;

        // Title
        sheet.MergedCells.Add(RangeRef.Parse("A1:O1"));
        sheet.Cells["A1"].Value = "2025 Annual Budget Tracker";
        sheet.Cells["A1"].Format = titleFormat;
        sheet.Rows[0] = 32;

        // Headers
        string[] months = { "Category", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "Total", "Budget" };
        for (int i = 0; i < months.Length; i++)
        {
            sheet.Cells[1, i].Value = months[i];
            sheet.Cells[1, i].Format = headerFormat;
        }
        sheet.Rows.Frozen = 2;

        // Income section
        sheet.Cells["A3"].Value = "INCOME"; sheet.Cells["A3"].Format = categoryFormat;
        string[] incomeItems = { "Salary", "Freelance", "Investments", "Other Income" };
        double[][] incomeData = {
            new[] { 5000.0, 5000, 5000, 5000, 5000, 5200, 5200, 5200, 5200, 5200, 5200, 5500 },
            new[] { 800.0, 1200, 600, 950, 1100, 750, 1300, 900, 1050, 800, 1400, 1100 },
            new[] { 200.0, 180, 220, 250, 190, 210, 230, 200, 240, 260, 220, 280 },
            new[] { 100.0, 50, 75, 0, 120, 0, 200, 0, 50, 0, 100, 150 }
        };
        double[] incomeBudgets = { 62000, 12000, 2700, 800 };

        for (int i = 0; i < incomeItems.Length; i++)
        {
            int row = 3 + i;
            sheet.Cells[row, 0].Value = incomeItems[i];
            for (int m = 0; m < 12; m++)
            {
                sheet.Cells[row, m + 1].Value = incomeData[i][m];
                sheet.Cells[row, m + 1].Format = currencyFormat;
            }
            sheet.Cells[row, 13].Formula = $"=SUM(B{row + 1}:M{row + 1})";
            sheet.Cells[row, 13].Format = totalFormat;
            sheet.Cells[row, 14].Value = incomeBudgets[i];
            sheet.Cells[row, 14].Format = currencyFormat;
        }

        // Income total
        sheet.Cells["A8"].Value = "Total Income"; sheet.Cells["A8"].Format = new Format { Bold = true, BackgroundColor = "#c8e6c9", Color = "#0f2e10" };
        for (int m = 1; m <= 14; m++)
        {
            sheet.Cells[7, m].Formula = $"=SUM({CellRef(3, m)}:{CellRef(6, m)})";
            sheet.Cells[7, m].Format = new Format { Bold = true, NumberFormat = "$#,##0.00", BackgroundColor = "#c8e6c9", Color = "#0f2e10" };
        }

        // Expense section
        sheet.Cells["A10"].Value = "EXPENSES"; sheet.Cells["A10"].Format = categoryFormat;
        string[] expenseItems = { "Rent/Mortgage", "Utilities", "Groceries", "Transportation", "Insurance", "Entertainment", "Dining Out", "Subscriptions", "Healthcare", "Savings" };
        double[][] expenseData = {
            new[] { 1500.0, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500, 1500 },
            new[] { 150.0, 165, 140, 130, 120, 180, 200, 195, 160, 145, 155, 170 },
            new[] { 400.0, 380, 420, 390, 410, 430, 380, 400, 440, 410, 390, 450 },
            new[] { 200.0, 180, 210, 190, 200, 220, 180, 200, 210, 190, 200, 230 },
            new[] { 300.0, 300, 300, 300, 300, 300, 300, 300, 300, 300, 300, 300 },
            new[] { 150.0, 200, 100, 180, 250, 300, 200, 150, 180, 120, 200, 350 },
            new[] { 200.0, 180, 250, 160, 220, 280, 300, 200, 240, 180, 200, 320 },
            new[] { 50.0, 50, 50, 55, 55, 55, 55, 60, 60, 60, 60, 60 },
            new[] { 100.0, 50, 75, 200, 50, 100, 50, 75, 300, 50, 100, 50 },
            new[] { 1000.0, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000 }
        };
        double[] expenseBudgets = { 18000, 1800, 4800, 2400, 3600, 2400, 2400, 660, 1200, 12000 };

        for (int i = 0; i < expenseItems.Length; i++)
        {
            int row = 10 + i;
            sheet.Cells[row, 0].Value = expenseItems[i];
            for (int m = 0; m < 12; m++)
            {
                sheet.Cells[row, m + 1].Value = expenseData[i][m];
                sheet.Cells[row, m + 1].Format = currencyFormat;
            }
            sheet.Cells[row, 13].Formula = $"=SUM(B{row + 1}:M{row + 1})";
            sheet.Cells[row, 13].Format = totalFormat;
            sheet.Cells[row, 14].Value = expenseBudgets[i];
            sheet.Cells[row, 14].Format = currencyFormat;
        }

        // Conditional formatting: highlight over-budget totals
        for (int i = 0; i < expenseItems.Length; i++)
        {
            int row = 10 + i;
            sheet.ConditionalFormats.Add(
                new RangeRef(new CellRef(row, 13), new CellRef(row, 13)),
                new GreaterThanRule { Value = expenseBudgets[i], Format = overBudgetFormat }
            );
        }

        // Expense total
        sheet.Cells["A21"].Value = "Total Expenses"; sheet.Cells["A21"].Format = new Format { Bold = true, BackgroundColor = "#ffcdd2", Color = "#c62828" };
        for (int m = 1; m <= 14; m++)
        {
            sheet.Cells[20, m].Formula = $"=SUM({CellRef(10, m)}:{CellRef(19, m)})";
            sheet.Cells[20, m].Format = new Format { Bold = true, NumberFormat = "$#,##0.00", BackgroundColor = "#ffcdd2", Color = "#c62828" };
        }

        // Net savings
        sheet.Cells["A23"].Value = "NET SAVINGS"; sheet.Cells["A23"].Format = new Format { Bold = true, FontSize = 12, BackgroundColor = "#fff9c4", Color = "#f57f17" };
        sheet.Rows[22] = 28;
        for (int m = 1; m <= 14; m++)
        {
            sheet.Cells[22, m].Formula = $"={CellRef(7, m)}-{CellRef(20, m)}";
            sheet.Cells[22, m].Format = new Format { Bold = true, NumberFormat = "$#,##0.00", FontSize = 12, BackgroundColor = "#fff9c4", Color = "#f57f17" };
        }

        sheet.EndUpdate();
    }

    void BuildTimesheet()
    {
        var sheet = workbook.AddSheet("Timesheet", 25, 10);

        sheet.BeginUpdate();

        var titleFormat = new Format { Bold = true, FontSize = 14, BackgroundColor = "#1565c0", Color = "#ffffff", TextAlign = TextAlign.Center };
        var headerFormat = new Format { Bold = true, BackgroundColor = "#e3f2fd", Color = "#005da1", TextAlign = TextAlign.Center };
        var hoursFormat = new Format { NumberFormat = "0.0", TextAlign = TextAlign.Center };
        var totalFormat = new Format { Bold = true, NumberFormat = "0.0", BackgroundColor = "#e8f5e9", Color = "#0f2e10", TextAlign = TextAlign.Center };
        var nameFormat = new Format { Bold = true };

        sheet.Columns[0] = 130;
        sheet.Columns[1] = 150;
        for (int i = 2; i <= 8; i++) sheet.Columns[i] = 70;
        sheet.Columns[9] = 80;

        // Title
        sheet.MergedCells.Add(RangeRef.Parse("A1:J1"));
        sheet.Cells["A1"].Value = "Weekly Timesheet - March 24-28, 2025";
        sheet.Cells["A1"].Format = titleFormat;
        sheet.Rows[0] = 32;

        // Headers
        string[] headers = { "Employee", "Project", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun", "Total" };
        for (int i = 0; i < headers.Length; i++)
        {
            sheet.Cells[1, i].Value = headers[i];
            sheet.Cells[1, i].Format = headerFormat;
        }
        sheet.Rows.Frozen = 2;

        string[] employees = { "Alice Chen", "Alice Chen", "Bob Martinez", "Bob Martinez", "Carol Davis", "Carol Davis", "Carol Davis", "David Kim", "David Kim" };
        string[] projects = { "Website Redesign", "API Integration", "Mobile App", "Database Migration", "Website Redesign", "Mobile App", "Code Review", "API Integration", "DevOps" };
        double[][] hours = {
            new[] { 6.0, 7, 8, 7, 6.5, 0, 0 },
            new[] { 2.0, 1, 0, 1.5, 1.5, 0, 0 },
            new[] { 4.0, 5, 4, 5, 4, 0, 0 },
            new[] { 4.0, 3, 4, 3, 4, 2, 0 },
            new[] { 3.0, 4, 3, 4, 3, 0, 0 },
            new[] { 3.0, 2, 3, 2, 3, 0, 0 },
            new[] { 2.0, 2, 2, 2, 2, 0, 0 },
            new[] { 5.0, 4, 5, 6, 5, 0, 0 },
            new[] { 3.0, 4, 3, 2, 3, 0, 2 }
        };

        for (int i = 0; i < employees.Length; i++)
        {
            int row = 2 + i;
            sheet.Cells[row, 0].Value = employees[i];
            sheet.Cells[row, 0].Format = nameFormat;
            sheet.Cells[row, 1].Value = projects[i];
            for (int d = 0; d < 7; d++)
            {
                sheet.Cells[row, d + 2].Value = hours[i][d];
                sheet.Cells[row, d + 2].Format = hoursFormat;
            }
            sheet.Cells[row, 9].Formula = $"=SUM(C{row + 1}:I{row + 1})";
            sheet.Cells[row, 9].Format = totalFormat;
        }

        // Conditional formatting: highlight overtime (> 8 hours/day)
        sheet.ConditionalFormats.Add(
            RangeRef.Parse($"C3:I{2 + employees.Length}"),
            new GreaterThanRule { Value = 8, Format = new Format { Color = "#c62828", BackgroundColor = "#ffebee", NumberFormat = "0.0", Bold = true, TextAlign = TextAlign.Center } }
        );

        // Daily totals
        int totalRow = 2 + employees.Length;
        sheet.Cells[totalRow, 0].Value = "Daily Total";
        sheet.Cells[totalRow, 0].Format = new Format { Bold = true, BackgroundColor = "#e8f5e9", Color = "#0f2e10" };
        sheet.Cells[totalRow, 1].Format = new Format { BackgroundColor = "#e8f5e9", Color = "#0f2e10" };
        for (int d = 2; d <= 9; d++)
        {
            sheet.Cells[totalRow, d].Formula = $"=SUM({CellRef(2, d)}:{CellRef(totalRow - 1, d)})";
            sheet.Cells[totalRow, d].Format = totalFormat;
        }

        // Per-employee summary
        int summaryStart = totalRow + 2;
        sheet.MergedCells.Add(new RangeRef(new CellRef(summaryStart, 0), new CellRef(summaryStart, 2)));
        sheet.Cells[summaryStart, 0].Value = "Employee Summary";
        sheet.Cells[summaryStart, 0].Format = new Format { Bold = true, FontSize = 12, BackgroundColor = "#1565c0", Color = "#ffffff" };
        sheet.Rows[summaryStart] = 28;

        sheet.Cells[summaryStart + 1, 0].Value = "Employee"; sheet.Cells[summaryStart + 1, 0].Format = headerFormat;
        sheet.Cells[summaryStart + 1, 1].Value = "Total Hours"; sheet.Cells[summaryStart + 1, 1].Format = headerFormat;
        sheet.Cells[summaryStart + 1, 2].Value = "Projects"; sheet.Cells[summaryStart + 1, 2].Format = headerFormat;

        string[] uniqueEmployees = { "Alice Chen", "Bob Martinez", "Carol Davis", "David Kim" };
        int[] projectCounts = { 2, 2, 3, 2 };
        string[] sumFormulas = {
            "=SUM(J3:J4)",
            "=SUM(J5:J6)",
            "=SUM(J7:J9)",
            "=SUM(J10:J11)"
        };

        for (int i = 0; i < uniqueEmployees.Length; i++)
        {
            int row = summaryStart + 2 + i;
            sheet.Cells[row, 0].Value = uniqueEmployees[i];
            sheet.Cells[row, 1].Formula = sumFormulas[i];
            sheet.Cells[row, 1].Format = totalFormat;
            sheet.Cells[row, 2].Value = (double)projectCounts[i];
            sheet.Cells[row, 2].Format = new Format { TextAlign = TextAlign.Center };
        }

        sheet.EndUpdate();
    }

    static string CellRef(int row, int col)
    {
        string colLetter = col < 26 ? ((char)('A' + col)).ToString() : $"{(char)('A' + col / 26 - 1)}{(char)('A' + col % 26)}";
        return $"{colLetter}{row + 1}";
    }
}
