<?php

namespace App\Http\Controllers;

use App\Models\Appointment;
use App\Models\Locker;
use App\Services\ReportService;
use Illuminate\Http\Request;
use Carbon\Carbon;
use Barryvdh\DomPDF\Facade\Pdf;

class ReportController extends Controller
{
    protected $reportService;

    public function __construct(ReportService $reportService)
    {
        $this->reportService = $reportService;
    }

    /**
     * Main reports dashboard
     */
    public function index()
    {
        return view('reports.index');
    }

    public function lockers()
    {
        return view('reports.lockers');
    }

    public function lockersReport(Request $request)
    {
        $query = Locker::query();

        if ($request->filled('status')) {
            $query->where('status', $request->status);
        }

        if ($request->filled('size')) {
            $query->where('locker_size', $request->size);
        }

        if ($request->filled('type')) {
            $query->where('locker_type', $request->type);
        }

        $count = $query->count();
        $lockers = $query->get();

        return response()->json([
            'data' => $lockers,
            'count' => $count
        ]);
    }

    /**
     * Export lockers report as XLS
     */
    public function exportLockers(Request $request)
    {
        $query = Locker::query();

        if ($request->filled('status')) {
            $query->where('status', $request->status);
        }

        if ($request->filled('size')) {
            $query->where('locker_size', $request->size);
        }

        if ($request->filled('type')) {
            $query->where('locker_type', $request->type);
        }

        $lockers = $query->get();

        $filename = "lockers_report_" . now()->format('Y-m-d_H-i-s') . ".xls";
        $headers = [
            'Content-Type' => 'application/vnd.ms-excel',
            'Content-Disposition' => "attachment; filename=\"$filename\"",
        ];

        $callback = function () use ($lockers) {
            $file = fopen('php://output', 'w');

            // Excel header for proper formatting
            echo pack("CCC", 0xEF, 0xBB, 0xBF);

            // CSV header
            fputcsv($file, [
                'Locker Number',
                'Locker Group',
                'Type',
                'Size',
                'Location',
                'Status',
                'Amount',
                'Created Date'
            ]);

            // CSV data
            foreach ($lockers as $locker) {
                fputcsv($file, [
                    $locker->locker_number ?? 'N/A',
                    'Group ' . ($locker->locker_group_id ?? 'N/A'),
                    $locker->locker_type ?? 'N/A',
                    $locker->locker_size ?? 'N/A',
                    $locker->locker_row_column ?? 'N/A',
                    ucfirst($locker->status ?? 'N/A'),
                    '$' . number_format($locker->locker_amount ?? 0, 2),
                    $locker->created_at ? $locker->created_at->format('Y-m-d H:i:s') : 'N/A'
                ]);
            }

            fclose($file);
        };

        return response()->stream($callback, 200, $headers);
    }

    /**
     * Export lockers report as PDF
     */
    public function exportLockersPDF(Request $request)
    {
        $query = Locker::query();

        if ($request->filled('status')) {
            $query->where('status', $request->status);
        }

        if ($request->filled('size')) {
            $query->where('locker_size', $request->size);
        }

        if ($request->filled('type')) {
            $query->where('locker_type', $request->type);
        }

        $lockers = $query->get();

        // Build filter summary for header
        $filterSummary = 'All Lockers';
        $activeFilters = [];

        if ($request->filled('status')) {
            $activeFilters[] = 'Status: ' . ucfirst($request->status);
        }
        if ($request->filled('size')) {
            $size = $request->size === 'Extra large' ? 'Extra Large' : ucfirst($request->size);
            $activeFilters[] = 'Size: ' . $size;
        }
        if ($request->filled('type')) {
            $activeFilters[] = 'Type: ' . $request->type;
        }

        if (!empty($activeFilters)) {
            $filterSummary = implode(', ', $activeFilters);
        }

        $data = [
            'lockers' => $lockers,
            'filterSummary' => $filterSummary,
            'generatedOn' => now()->format('Y-m-d H:i:s'),
            'totalCount' => $lockers->count()
        ];

        $filename = "lockers_report_" . now()->format('Y-m-d_H-i-s') . ".pdf";

        $pdf = PDF::loadView('reports.lockers-pdf', $data);
        return $pdf->download($filename);
    }

    /**
     * Export lockers report as TXT
     */
    public function exportLockersTXT(Request $request)
    {
        $query = Locker::query();

        if ($request->filled('status')) {
            $query->where('status', $request->status);
        }

        if ($request->filled('size')) {
            $query->where('locker_size', $request->size);
        }

        if ($request->filled('type')) {
            $query->where('locker_type', $request->type);
        }

        $lockers = $query->get();

        // Build filter summary
        $filterParts = [];
        if ($request->filled('status')) $filterParts[] = 'Status: ' . ucfirst($request->status);
        if ($request->filled('size')) {
            $size = $request->size === 'Extra large' ? 'Extra Large' : ucfirst($request->size);
            $filterParts[] = 'Size: ' . $size;
        }
        if ($request->filled('type')) $filterParts[] = 'Type: ' . $request->type;

        $filterSummary = empty($filterParts) ? 'All Lockers' : implode(', ', $filterParts);

        $filename = "lockers_report_" . date('Y-m-d_H-i') . ".txt";
        $headers = [
            'Content-Type' => 'text/plain',
            'Content-Disposition' => "attachment; filename=\"$filename\"",
        ];

        $callback = function () use ($lockers, $filterSummary) {
            echo "LOCKERS REPORT\n";
            echo "================\n\n";
            echo "Generated on: " . now()->format('Y-m-d H:i') . "\n";
            echo "Filters: $filterSummary\n";
            echo "Total Lockers: " . $lockers->count() . "\n\n";
            echo "LOCKER DETAILS\n";
            echo "==============\n\n";

            // Table header
            echo str_pad("Locker Number", 15) . " | ";
            echo str_pad("Type", 12) . " | ";
            echo str_pad("Size", 10) . " | ";
            echo str_pad("Status", 12) . " | ";
            echo str_pad("Amount", 10) . " | ";
            echo "Created Date\n";
            echo str_repeat("-", 70) . "\n";

            // Data rows
            foreach ($lockers as $locker) {
                echo str_pad($locker->locker_number ?? 'N/A', 15) . " | ";
                echo str_pad($locker->locker_type ?? 'N/A', 12) . " | ";
                echo str_pad($locker->locker_size ?? 'N/A', 10) . " | ";
                echo str_pad(ucfirst($locker->status ?? 'N/A'), 12) . " | ";
                echo str_pad('$' . number_format($locker->locker_amount ?? 0, 2), 10) . " | ";
                echo ($locker->created_at ? $locker->created_at->format('Y-m-d H:i:s') : 'N/A') . "\n";
            }

            echo "\n\nReport generated by MaySafeBox System\n";
        };

        return response()->stream($callback, 200, $headers);
    }

    public function appointments()
    {
        return view('reports.appointments');
    }

    public function appointmentsReport(Request $request)
    {
        // return response()->json([
        //     'status' => 'success',
        //     'message' => 'Appointments report generated successfully',
        //     'data' => $request->all()
        // ]);

        $appointments = Appointment::query();

        if ($request->filled('status')) {
            $appointments->where('status', $request->status);
        }

        // Filter by date range using from_date and to_date
        if ($request->filled('from_date')) {
            $appointments->whereDate('appointment_date', '>=', $request->from_date);
        }
        
        if ($request->filled('to_date')) {
            $appointments->whereDate('appointment_date', '<=', $request->to_date);
        }

        $appointments = $appointments->with(['client'])->get();
        return response()->json([
            'data' => $appointments,
            'count' => $appointments->count()
        ]);
    }

    /**
     * Analytics dashboard
     */
    public function analytics(Request $request)
    {
        $period = $request->get('period', '30days');
        $analytics = $this->reportService->generateDashboardAnalytics($period);

        return view('reports.analytics', compact('analytics', 'period'));
    }

    /**
     * Attendance report page
     */
    public function attendance(Request $request)
    {
        $startDate = $request->get('start_date', now()->subDays(30)->format('Y-m-d'));
        $endDate = $request->get('end_date', now()->format('Y-m-d'));

        $filters = [
            'employee_id' => $request->get('employee_id'),
            'department_id' => $request->get('department_id'),
            'status' => $request->get('status')
        ];

        $report = $this->reportService->generateAttendanceReport($startDate, $endDate, $filters);

        // Get filter options
        $employees = \App\Models\User::byTenant()->active()->get();
        $departments = \App\Models\Department::byTenant()->get();

        return view('reports.attendance', compact('report', 'startDate', 'endDate', 'filters', 'employees', 'departments'));
    }

    /**
     * Transit report page
     */
    public function transit(Request $request)
    {
        $startDate = $request->get('start_date', now()->subDays(30)->format('Y-m-d'));
        $endDate = $request->get('end_date', now()->format('Y-m-d'));

        $filters = [
            'employee_id' => $request->get('employee_id'),
            'client_id' => $request->get('client_id'),
            'status' => $request->get('status')
        ];

        $report = $this->reportService->generateTransitReport($startDate, $endDate, $filters);

        // Get filter options
        $employees = \App\Models\User::byTenant()->active()->get();
        $clients = \App\Models\Client::byTenant()->active()->get();

        return view('reports.transit', compact('report', 'startDate', 'endDate', 'filters', 'employees', 'clients'));
    }

    /**
     * Geofence violations report page
     */
    public function geofence(Request $request)
    {
        $startDate = $request->get('start_date', now()->subDays(30)->format('Y-m-d'));
        $endDate = $request->get('end_date', now()->format('Y-m-d'));

        $filters = [
            'geofence_id' => $request->get('geofence_id'),
            'employee_id' => $request->get('employee_id'),
            'violation_type' => $request->get('violation_type')
        ];

        $report = $this->reportService->generateGeofenceReport($startDate, $endDate, $filters);

        // Get filter options
        $geofences = \App\Models\Geofence::byTenant()->active()->get();
        $employees = \App\Models\User::byTenant()->active()->get();

        return view('reports.geofence', compact('report', 'startDate', 'endDate', 'filters', 'geofences', 'employees'));
    }

    /**
     * Employee performance report
     */
    public function employeePerformance(Request $request)
    {
        $period = $request->get('period', '30days');
        $employeeId = $request->get('employee_id');

        $endDate = now();
        $startDate = match ($period) {
            '7days' => now()->subDays(7),
            '30days' => now()->subDays(30),
            '90days' => now()->subDays(90),
            '1year' => now()->subYear(),
            default => now()->subDays(30)
        };

        if ($employeeId) {
            $employee = \App\Models\User::byTenant()->findOrFail($employeeId);
            $performance = $this->getEmployeeDetailedPerformance($employee, $startDate, $endDate);
        } else {
            $performance = $this->getAllEmployeesPerformance($startDate, $endDate);
        }

        $employees = \App\Models\User::byTenant()->active()->get();

        return view('reports.employee-performance', compact('performance', 'period', 'startDate', 'endDate', 'employees', 'employeeId'));
    }

    /**
     * Client performance report
     */
    public function clientPerformance(Request $request)
    {
        $period = $request->get('period', '30days');
        $clientId = $request->get('client_id');

        $endDate = now();
        $startDate = match ($period) {
            '7days' => now()->subDays(7),
            '30days' => now()->subDays(30),
            '90days' => now()->subDays(90),
            '1year' => now()->subYear(),
            default => now()->subDays(30)
        };

        if ($clientId) {
            $client = \App\Models\Client::byTenant()->findOrFail($clientId);
            $performance = $this->getClientDetailedPerformance($client, $startDate, $endDate);
        } else {
            $performance = $this->getAllClientsPerformance($startDate, $endDate);
        }

        $clients = \App\Models\Client::byTenant()->active()->get();

        return view('reports.client-performance', compact('performance', 'period', 'startDate', 'endDate', 'clients', 'clientId'));
    }

    /**
     * Export attendance report
     */
    public function exportAttendance(Request $request)
    {
        $startDate = $request->get('start_date', now()->subDays(30)->format('Y-m-d'));
        $endDate = $request->get('end_date', now()->format('Y-m-d'));

        $filters = [
            'employee_id' => $request->get('employee_id'),
            'department_id' => $request->get('department_id'),
            'status' => $request->get('status')
        ];

        $report = $this->reportService->generateAttendanceReport($startDate, $endDate, $filters);

        $filename = "attendance_report_{$startDate}_to_{$endDate}.csv";
        $headers = [
            'Content-Type' => 'text/csv',
            'Content-Disposition' => "attachment; filename=\"$filename\"",
        ];

        $callback = function () use ($report) {
            $file = fopen('php://output', 'w');

            // CSV header
            fputcsv($file, [
                'Employee ID',
                'Employee Name',
                'Department',
                'Date',
                'Status',
                'Check In',
                'Check Out',
                'Working Hours',
                'Overtime Hours'
            ]);

            // CSV data
            foreach ($report['details'] as $attendance) {
                fputcsv($file, [
                    $attendance->employee->profile->employee_id ?? 'N/A',
                    $attendance->employee->full_name,
                    $attendance->employee->departments->pluck('name')->join(', ') ?: 'N/A',
                    $attendance->date,
                    $attendance->status,
                    $attendance->check_in?->format('h:i A') ?? 'N/A',
                    $attendance->check_out?->format('h:i A') ?? 'N/A',
                    $attendance->working_hours ?? 'N/A',
                    $attendance->overtime_hours ?? 'N/A'
                ]);
            }

            fclose($file);
        };

        return response()->stream($callback, 200, $headers);
    }

    /**
     * Export transit report
     */
    public function exportTransit(Request $request)
    {
        $startDate = $request->get('start_date', now()->subDays(30)->format('Y-m-d'));
        $endDate = $request->get('end_date', now()->format('Y-m-d'));

        $filters = [
            'employee_id' => $request->get('employee_id'),
            'client_id' => $request->get('client_id'),
            'status' => $request->get('status')
        ];

        $report = $this->reportService->generateTransitReport($startDate, $endDate, $filters);

        $filename = "transit_report_{$startDate}_to_{$endDate}.csv";
        $headers = [
            'Content-Type' => 'text/csv',
            'Content-Disposition' => "attachment; filename=\"$filename\"",
        ];

        $callback = function () use ($report) {
            $file = fopen('php://output', 'w');

            // CSV header
            fputcsv($file, [
                'Employee ID',
                'Employee Name',
                'Client',
                'From Location',
                'To Location',
                'Start Time',
                'Estimated Arrival',
                'Actual Arrival',
                'Distance (km)',
                'Status',
                'Vehicle',
                'Purpose'
            ]);

            // CSV data
            foreach ($report['details'] as $transit) {
                fputcsv($file, [
                    $transit->employee->profile->employee_id ?? 'N/A',
                    $transit->employee->full_name,
                    $transit->client?->name ?? 'N/A',
                    $transit->from_location,
                    $transit->to_location,
                    $transit->start_time->format('Y-m-d H:i'),
                    $transit->estimated_arrival?->format('Y-m-d H:i') ?? 'N/A',
                    $transit->actual_arrival?->format('Y-m-d H:i') ?? 'N/A',
                    $transit->distance ?? 'N/A',
                    $transit->status,
                    $transit->vehicle ?? 'N/A',
                    $transit->purpose ?? 'N/A'
                ]);
            }

            fclose($file);
        };

        return response()->stream($callback, 200, $headers);
    }

    /**
     * Export geofence violations report
     */
    public function exportGeofence(Request $request)
    {
        $startDate = $request->get('start_date', now()->subDays(30)->format('Y-m-d'));
        $endDate = $request->get('end_date', now()->format('Y-m-d'));

        $filters = [
            'geofence_id' => $request->get('geofence_id'),
            'employee_id' => $request->get('employee_id'),
            'violation_type' => $request->get('violation_type')
        ];

        $report = $this->reportService->generateGeofenceReport($startDate, $endDate, $filters);

        $filename = "geofence_violations_report_{$startDate}_to_{$endDate}.csv";
        $headers = [
            'Content-Type' => 'text/csv',
            'Content-Disposition' => "attachment; filename=\"$filename\"",
        ];

        $callback = function () use ($report) {
            $file = fopen('php://output', 'w');

            // CSV header
            fputcsv($file, [
                'Employee ID',
                'Employee Name',
                'Geofence',
                'Violation Type',
                'Violation Time',
                'Latitude',
                'Longitude',
                'Status',
                'Resolution Notes',
                'Action Taken',
                'Resolved By',
                'Resolved At'
            ]);

            // CSV data
            foreach ($report['details'] as $violation) {
                fputcsv($file, [
                    $violation->employee->profile->employee_id ?? 'N/A',
                    $violation->employee->full_name,
                    $violation->geofence->name,
                    $violation->violation_type,
                    $violation->violation_time->format('Y-m-d H:i'),
                    $violation->latitude,
                    $violation->longitude,
                    $violation->is_resolved ? 'Resolved' : 'Unresolved',
                    $violation->resolution_notes ?? 'N/A',
                    $violation->action_taken ?? 'N/A',
                    $violation->resolvedBy?->full_name ?? 'N/A',
                    $violation->resolved_at?->format('Y-m-d H:i') ?? 'N/A'
                ]);
            }

            fclose($file);
        };

        return response()->stream($callback, 200, $headers);
    }

    /**
     * API endpoint for real-time dashboard data
     */
    public function dashboardData(Request $request)
    {
        $period = $request->get('period', '30days');
        $analytics = $this->reportService->generateDashboardAnalytics($period);

        return response()->json([
            'success' => true,
            'data' => $analytics
        ]);
    }

    /**
     * API endpoint for chart data
     */
    public function chartData(Request $request)
    {
        $type = $request->get('type');
        $period = $request->get('period', '30days');

        $endDate = now();
        $startDate = match ($period) {
            '7days' => now()->subDays(7),
            '30days' => now()->subDays(30),
            '90days' => now()->subDays(90),
            '1year' => now()->subYear(),
            default => now()->subDays(30)
        };

        $data = match ($type) {
            'attendance_trends' => $this->getAttendanceChartData($startDate, $endDate),
            'transit_trends' => $this->getTransitChartData($startDate, $endDate),
            'violation_trends' => $this->getViolationChartData($startDate, $endDate),
            'department_performance' => $this->getDepartmentChartData($startDate, $endDate),
            default => []
        };

        return response()->json([
            'success' => true,
            'data' => $data
        ]);
    }

    // Helper methods for detailed performance reports
    private function getEmployeeDetailedPerformance($employee, $startDate, $endDate)
    {
        $attendances = \App\Models\Attendance::where('employee_id', $employee->id)
            ->whereBetween('date', [$startDate, $endDate])
            ->get();

        $transits = \App\Models\Transit::where('employee_id', $employee->id)
            ->whereBetween('start_time', [$startDate, $endDate])
            ->get();

        $violations = \App\Models\GeofenceViolation::where('employee_id', $employee->id)
            ->whereBetween('violation_time', [$startDate, $endDate])
            ->get();

        return [
            'employee' => $employee,
            'attendance_summary' => $this->reportService->calculateAttendanceSummary($attendances),
            'transit_summary' => $this->reportService->calculateTransitSummary($transits),
            'violation_summary' => $this->reportService->calculateViolationSummary($violations),
            'performance_score' => $this->calculateEmployeePerformanceScore($attendances, $transits, $violations)
        ];
    }

    private function getAllEmployeesPerformance($startDate, $endDate)
    {
        $employees = \App\Models\User::byTenant()->active()->get();
        $performanceData = [];

        foreach ($employees as $employee) {
            $performanceData[] = $this->getEmployeeDetailedPerformance($employee, $startDate, $endDate);
        }

        // Sort by performance score
        usort($performanceData, function ($a, $b) {
            return $b['performance_score'] <=> $a['performance_score'];
        });

        return $performanceData;
    }

    private function getClientDetailedPerformance($client, $startDate, $endDate)
    {
        $transits = \App\Models\Transit::where('client_id', $client->id)
            ->whereBetween('start_time', [$startDate, $endDate])
            ->get();

        $violations = \App\Models\GeofenceViolation::whereHas('geofence', function ($query) use ($client) {
            $query->where('client_id', $client->id);
        })->whereBetween('violation_time', [$startDate, $endDate])->get();

        return [
            'client' => $client,
            'transit_summary' => $this->reportService->calculateTransitSummary($transits),
            'violation_summary' => $this->reportService->calculateViolationSummary($violations),
            'assigned_employees' => $transits->pluck('employee_id')->unique()->count(),
            'performance_metrics' => $this->calculateClientPerformanceMetrics($transits, $violations)
        ];
    }

    private function getAllClientsPerformance($startDate, $endDate)
    {
        $clients = \App\Models\Client::byTenant()->active()->get();
        $performanceData = [];

        foreach ($clients as $client) {
            $performanceData[] = $this->getClientDetailedPerformance($client, $startDate, $endDate);
        }

        return $performanceData;
    }

    private function calculateEmployeePerformanceScore($attendances, $transits, $violations)
    {
        $attendanceScore = 0;
        $transitScore = 0;
        $violationScore = 0;

        // Attendance score (40% weight)
        if ($attendances->isNotEmpty()) {
            $presentCount = $attendances->where('status', 'present')->count();
            $attendanceRate = ($presentCount / $attendances->count()) * 100;
            $attendanceScore = ($attendanceRate / 100) * 40;
        }

        // Transit score (35% weight)
        if ($transits->isNotEmpty()) {
            $completedCount = $transits->where('status', 'arrived')->count();
            $completionRate = ($completedCount / $transits->count()) * 100;
            $transitScore = ($completionRate / 100) * 35;
        }

        // Violation score (25% weight) - fewer violations = higher score
        $violationScore = max(0, 25 - ($violations->count() * 2));

        return round($attendanceScore + $transitScore + $violationScore, 2);
    }

    private function calculateClientPerformanceMetrics($transits, $violations)
    {
        return [
            'service_efficiency' => $transits->isNotEmpty()
                ? round(($transits->where('status', 'arrived')->count() / $transits->count()) * 100, 2)
                : 0,
            'security_compliance' => 100 - min(100, ($violations->count() * 10)),
            'employee_utilization' => $transits->pluck('employee_id')->unique()->count(),
            'total_distance' => $transits->sum('distance')
        ];
    }

    // Chart data methods
    private function getAttendanceChartData($startDate, $endDate)
    {
        $attendances = \App\Models\Attendance::byTenant()
            ->whereBetween('date', [$startDate, $endDate])
            ->get()
            ->groupBy('date');

        $labels = [];
        $presentData = [];
        $absentData = [];

        $period = \Carbon\CarbonPeriod::create($startDate, $endDate);
        foreach ($period as $date) {
            $dateStr = $date->format('Y-m-d');
            $dayAttendances = $attendances->get($dateStr, collect());

            $labels[] = $date->format('M d');
            $presentData[] = $dayAttendances->where('status', 'present')->count();
            $absentData[] = $dayAttendances->where('status', 'absent')->count();
        }

        return [
            'labels' => $labels,
            'datasets' => [
                [
                    'label' => 'Present',
                    'data' => $presentData,
                    'backgroundColor' => 'rgba(75, 192, 192, 0.2)',
                    'borderColor' => 'rgba(75, 192, 192, 1)',
                    'borderWidth' => 1
                ],
                [
                    'label' => 'Absent',
                    'data' => $absentData,
                    'backgroundColor' => 'rgba(255, 99, 132, 0.2)',
                    'borderColor' => 'rgba(255, 99, 132, 1)',
                    'borderWidth' => 1
                ]
            ]
        ];
    }

    private function getTransitChartData($startDate, $endDate)
    {
        $transits = \App\Models\Transit::byTenant()
            ->whereBetween('start_time', [$startDate, $endDate])
            ->get()
            ->groupBy(function ($transit) {
                return \Carbon\Carbon::parse($transit->start_time)->format('Y-m-d');
            });

        $labels = [];
        $completedData = [];
        $delayedData = [];

        $period = \Carbon\CarbonPeriod::create($startDate, $endDate);
        foreach ($period as $date) {
            $dateStr = $date->format('Y-m-d');
            $dayTransits = $transits->get($dateStr, collect());

            $labels[] = $date->format('M d');
            $completedData[] = $dayTransits->where('status', 'arrived')->count();
            $delayedData[] = $dayTransits->where('status', 'delayed')->count();
        }

        return [
            'labels' => $labels,
            'datasets' => [
                [
                    'label' => 'Completed',
                    'data' => $completedData,
                    'backgroundColor' => 'rgba(54, 162, 235, 0.2)',
                    'borderColor' => 'rgba(54, 162, 235, 1)',
                    'borderWidth' => 1
                ],
                [
                    'label' => 'Delayed',
                    'data' => $delayedData,
                    'backgroundColor' => 'rgba(255, 206, 86, 0.2)',
                    'borderColor' => 'rgba(255, 206, 86, 1)',
                    'borderWidth' => 1
                ]
            ]
        ];
    }

    private function getViolationChartData($startDate, $endDate)
    {
        $violations = \App\Models\GeofenceViolation::byTenant()
            ->whereBetween('violation_time', [$startDate, $endDate])
            ->get()
            ->groupBy(function ($violation) {
                return \Carbon\Carbon::parse($violation->violation_time)->format('Y-m-d');
            });

        $labels = [];
        $violationData = [];

        $period = \Carbon\CarbonPeriod::create($startDate, $endDate);
        foreach ($period as $date) {
            $dateStr = $date->format('Y-m-d');
            $dayViolations = $violations->get($dateStr, collect());

            $labels[] = $date->format('M d');
            $violationData[] = $dayViolations->count();
        }

        return [
            'labels' => $labels,
            'datasets' => [
                [
                    'label' => 'Violations',
                    'data' => $violationData,
                    'backgroundColor' => 'rgba(255, 99, 132, 0.2)',
                    'borderColor' => 'rgba(255, 99, 132, 1)',
                    'borderWidth' => 1
                ]
            ]
        ];
    }

    private function getDepartmentChartData($startDate, $endDate)
    {
        $attendances = \App\Models\Attendance::byTenant()
            ->with(['employee.departments'])
            ->whereBetween('date', [$startDate, $endDate])
            ->get();

        $departmentData = $attendances->groupBy(function ($attendance) {
            return $attendance->employee->departments->pluck('name')->join(', ') ?: 'No Department';
        })->map(function ($deptAttendances) {
            $total = $deptAttendances->count();
            $present = $deptAttendances->where('status', 'present')->count();
            return [
                'total' => $total,
                'present' => $present,
                'rate' => $total > 0 ? round(($present / $total) * 100, 2) : 0
            ];
        });

        return [
            'labels' => $departmentData->keys(),
            'datasets' => [
                [
                    'label' => 'Attendance Rate (%)',
                    'data' => $departmentData->pluck('rate'),
                    'backgroundColor' => 'rgba(75, 192, 192, 0.2)',
                    'borderColor' => 'rgba(75, 192, 192, 1)',
                    'borderWidth' => 1
                ]
            ]
        ];
    }

    /**
     * Patrol report page
     */
    public function patrol(Request $request)
    {
        $startDate = $request->get('start_date', now()->subDays(30)->format('Y-m-d'));
        $endDate = $request->get('end_date', now()->format('Y-m-d'));

        $filters = [
            'client_id' => $request->get('client_id'),
            'service_id' => $request->get('service_id'),
            'employee_id' => $request->get('employee_id'),
            'report_type' => $request->get('report_type', 'summary')
        ];

        // Get filter options
        $clients = \App\Models\Client::byTenant()->active()->pluck('name', 'id');
        $services = \App\Models\ClientService::byTenant()->active()->pluck('service_name', 'id');
        $employees = \App\Models\User::byTenant()->active()->pluck('full_name', 'id');

        return view('reports.patrol', compact(
            'startDate',
            'endDate',
            'filters',
            'clients',
            'services',
            'employees'
        ));
    }

    /**
     * Export appointments report as TXT file
     */
    public function exportAppointmentsTXT(Request $request)
    {
        $appointments = Appointment::query();

        // Apply same filters as appointmentsReport
        if ($request->filled('status')) {
            $appointments->where('status', $request->status);
        }

        if ($request->filled('from_date')) {
            $appointments->whereDate('appointment_date', '>=', $request->from_date);
        }
        
        if ($request->filled('to_date')) {
            $appointments->whereDate('appointment_date', '<=', $request->to_date);
        }

        $appointments = $appointments->with(['client'])->get();

        $filename = 'appointments_report_' . date('Y-m-d_H-i-s') . '.txt';
        
        $headers = [
            'Content-Type' => 'text/plain',
            'Content-Disposition' => 'attachment; filename="' . $filename . '"',
        ];

        $callback = function() use ($appointments, $request) {
            $file = fopen('php://output', 'w');
            
            // Header
            fwrite($file, "APPOINTMENTS REPORT\n");
            fwrite($file, "Generated on: " . now()->format('Y-m-d H:i:s') . "\n");
            fwrite($file, "Total Appointments: " . $appointments->count() . "\n");
            
            // Filters applied
            if ($request->filled('from_date') || $request->filled('to_date') || $request->filled('status')) {
                fwrite($file, "\nFilters Applied:\n");
                if ($request->filled('from_date')) {
                    fwrite($file, "- From Date: " . $request->from_date . "\n");
                }
                if ($request->filled('to_date')) {
                    fwrite($file, "- To Date: " . $request->to_date . "\n");
                }
                if ($request->filled('status')) {
                    fwrite($file, "- Status: " . ucfirst($request->status) . "\n");
                }
            }
            
            fwrite($file, "\n" . str_repeat("=", 80) . "\n");
            fwrite($file, sprintf("%-20s %-20s %-15s %-15s %-12s %-10s\n", 
                'Client', 'Title', 'Date', 'Time', 'Status', 'Contact'));
            fwrite($file, str_repeat("-", 80) . "\n");
            
            // Data rows
            foreach ($appointments as $appointment) {
                fwrite($file, sprintf("%-20s %-20s %-15s %-15s %-12s $%-9.2f\n",
                    $appointment->client->name ?? 'N/A',
                    $appointment->appointment_title?? 'N/A',
                    $appointment->appointment_date ? $appointment->appointment_date->format('Y-m-d') : 'N/A',
                    $appointment->appointment_time ?? 'N/A',
                    ucfirst($appointment->status ?? 'N/A'),
                    floatval($appointment->client->phone ?? 'N/A')
                ));
            }
            
            fwrite($file, str_repeat("=", 80) . "\n");
            fwrite($file, "\nReport End\n");
            
            fclose($file);
        };

        return response()->stream($callback, 200, $headers);
    }
}
