<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;

class CsvExportController extends Controller
{
    public function downloadCSV()
    {
        // Execute SQL query
        $results = DB::select("
            SELECT 
    l.id, 
    l.title, 
    l.lead_time, 
    l.short_description, 
    l.learning_goals, 
    l.review, 
    l.certification, 
    l.extra_information, 
    l.target_audience, 
    l.quality_standards, 
    l.contract_agreements,
    (
        SELECT CONCAT('https://mijnggzbackend.ggzecademy.nl/storage/', m.id, '/', m.file_name) 
        FROM media m 
        WHERE m.model_id = l.id AND m.collection_name = 'learning_products_covers' 
        LIMIT 1
    ) AS cover,
    (
        SELECT CONCAT('https://mijnggzbackend.ggzecademy.nl/storage/', m.id, '/', m.file_name) 
        FROM media m 
        WHERE m.model_id = l.id AND m.collection_name = 'learning_products_tiles' 
        LIMIT 1
    ) AS thumb,
    (
        SELECT GROUP_CONCAT(CONCAT(i.title, '-', ac.credits))
        FROM filter_items_associations a  
        LEFT JOIN filter_items AS i ON i.id = a.filter_item_id 
        LEFT JOIN accreditations AS ac ON ac.id = a.filter_items_associations_id 
        WHERE ac.learning_product_id = l.id AND i.filter_id = 10
    ) AS titles_and_credits
FROM 
    learning_products l 
WHERE 
    l.published = 1 AND l.deleted_at IS NULL 
GROUP BY 
    l.title, l.id, l.lead_time, l.short_description, l.learning_goals, l.review, 
    l.certification, l.extra_information, l.target_audience, l.quality_standards, l.contract_agreements
        ");

        // Format the results into CSV
        $csvOutput = $this->formatToCsv($results);

        // Send the CSV file to the client
        $filename = "export.csv";
        return response($csvOutput, 200)
            ->header('Content-Type', 'text/csv')
            ->header('Content-Disposition', "attachment; filename=$filename");
    }

    protected function formatToCsv($data)
    {
        $handle = fopen('php://temp', 'w');
        
        // Add headers for CSV (if needed)
        fputcsv($handle, [
            'ID', 'Title', 'Lead Time', 'Short Description',
            'Learning Goals', 'Review', 'Certification', 'Extra Information',
            'Target Audience', 'Quality Standards', 'Contract Agreements', 
            'Cover', 'Thumb', 'Titles and Credits'
        ]);

        foreach ($data as $row) {
            fputcsv($handle, [
                $row->id,
                strip_tags($row->title),
                strip_tags($row->lead_time),
                strip_tags($row->short_description),
                strip_tags($row->learning_goals),
                strip_tags($row->review),
                strip_tags($row->certification),
                strip_tags($row->extra_information),
                strip_tags($row->target_audience),
                strip_tags($row->quality_standards),
                strip_tags($row->contract_agreements),
                $row->cover,
                $row->thumb,
                $row->titles_and_credits
            ]);
        }

        rewind($handle);
        $contents = stream_get_contents($handle);
        fclose($handle);

        return $contents;
    }
}
