<?php
// superadmin/download-template.php
session_start();
require_once '../includes/config.php';
require_once '../includes/sa_auth.php';
requireSuperAdmin();

// Requires PhpSpreadsheet
require_once dirname(__DIR__) . '/vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Style\Font;
use PhpOffice\PhpSpreadsheet\Style\Border;

$spreadsheet = new Spreadsheet();
$sheet       = $spreadsheet->getActiveSheet();
$sheet->setTitle('Invitees');

// ── Headers ──
$sheet->setCellValue('A1', 'Token');
$sheet->setCellValue('B1', 'Name');
$sheet->setCellValue('C1', 'Notes (optional - ignored on import)');

// Header style
$headerStyle = [
    'font'      => ['bold' => true, 'color' => ['rgb' => 'FFFFFF'], 'size' => 12],
    'fill'      => ['fillType' => Fill::FILL_SOLID, 'startColor' => ['rgb' => 'B8A67C']],
    'alignment' => ['horizontal' => Alignment::HORIZONTAL_CENTER],
    'borders'   => ['allBorders' => ['borderStyle' => Border::BORDER_THIN, 'color' => ['rgb' => '999999']]],
];
$sheet->getStyle('A1:C1')->applyFromArray($headerStyle);

// ── Sample rows ──
$samples = [
    ['family_smith',    'John Smith',     'Invited with wife'],
    ['family_smith',    'Jane Smith',     ''],
    ['friends_group1',  'Alice Johnson',  ''],
    ['friends_group1',  'Bob Williams',   ''],
    ['family_jones',    'Michael Jones',  'Groom\'s uncle'],
    ['family_jones',    'Sarah Jones',    ''],
    ['colleagues_a',    'Emily Davis',    'Work colleague'],
    ['vip_001',         'Father Georges', 'VIP'],
];

$rowNum = 2;
foreach ($samples as $row) {
    $sheet->setCellValue("A$rowNum", $row[0]);
    $sheet->setCellValue("B$rowNum", $row[1]);
    $sheet->setCellValue("C$rowNum", $row[2]);
    $rowNum++;
}

// ── Column widths ──
$sheet->getColumnDimension('A')->setWidth(25);
$sheet->getColumnDimension('B')->setWidth(30);
$sheet->getColumnDimension('C')->setWidth(40);

// ── Data rows style ──
$dataStyle = [
    'borders' => ['allBorders' => ['borderStyle' => Border::BORDER_THIN, 'color' => ['rgb' => 'DDDDDD']]],
    'fill'    => ['fillType' => Fill::FILL_SOLID, 'startColor' => ['rgb' => 'FAFAFA']],
];
$sheet->getStyle('A2:C' . ($rowNum-1))->applyFromArray($dataStyle);

// ── Instructions sheet ──
$sheet2 = $spreadsheet->createSheet();
$sheet2->setTitle('Instructions');
$sheet2->setCellValue('A1', 'HOW TO USE THIS TEMPLATE');
$sheet2->getStyle('A1')->applyFromArray(['font' => ['bold' => true, 'size' => 14, 'color' => ['rgb' => 'B8A67C']]]);
$instructions = [
    ['A3', 'Column A (Token):'],
    ['B3', 'A unique group identifier. All invitees in the same group share one invitation link.'],
    ['A4', ''],
    ['B4', 'Example: All members of the "Smith family" get token "family_smith". The link will be: site.com/?token=family_smith'],
    ['A6', 'Column B (Name):'],
    ['B6', 'Full name of the invitee (as it will appear on their invitation).'],
    ['A8', 'Rules:'],
    ['B8', '- Do NOT change column headers (Row 1)'],
    ['B9', '- Each row = one person'],
    ['B10','- Multiple people can share the same token'],
    ['B11','- Tokens should use only letters, numbers, and underscores (no spaces)'],
    ['B12','- Column C is ignored during import'],
    ['A14','Contact:'],
    ['B14','Agenz | 71998578'],
];
foreach ($instructions as [$cell, $val]) {
    $sheet2->setCellValue($cell, $val);
}
$sheet2->getColumnDimension('A')->setWidth(20);
$sheet2->getColumnDimension('B')->setWidth(70);

// ── Output ──
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="invitees_template.xlsx"');
header('Cache-Control: max-age=0');

$writer = new Xlsx($spreadsheet);
$writer->save('php://output');
exit;
