# Script simples para criar e fazer deploy dos endpoints faltantes
$ErrorActionPreference = "Stop"

$baseDir = "C:\Users\raild\MEDICONNECT 13-11\riseup-squad18\supabase\functions"

$endpoints = @(
    "availability-create",
    "availability-update",
    "availability-delete",
    "availability-slots",
    "exceptions-list",
    "exceptions-create",
    "exceptions-delete",
    "waitlist-match",
    "waitlist-remove",
    "queue-checkin",
    "notifications-subscription",
    "reports-list-extended",
    "reports-export-csv",
    "reports-integrity-check",
    "doctor-summary",
    "doctor-occupancy",
    "doctor-delay-suggestion",
    "patients-history",
    "patients-preferences",
    "patients-portal",
    "analytics-heatmap",
    "analytics-demand-curve",
    "analytics-ranking-reasons",
    "analytics-monthly-no-show",
    "analytics-specialty-heatmap",
    "analytics-custom-report",
    "accessibility-preferences",
    "audit-list",
    "system-health-check",
    "system-cache-rebuild",
    "system-cron-runner"
)

$simpleTemplate = @'
import { createClient } from "https://esm.sh/@supabase/supabase-js@2";

const corsHeaders = {
  "Access-Control-Allow-Origin": "*",
  "Access-Control-Allow-Headers": "authorization, x-client-info, apikey, content-type",
};

Deno.serve(async (req) => {
  if (req.method === "OPTIONS") return new Response("ok", { headers: corsHeaders });
  
  try {
    const authHeader = req.headers.get("Authorization");
    const supabase = createClient(
      Deno.env.get("SUPABASE_URL")!,
      Deno.env.get("SUPABASE_ANON_KEY")!,
      { global: { headers: { Authorization: authHeader! } } }
    );
    
    const { data: { user } } = await supabase.auth.getUser();
    if (!user) throw new Error("Unauthorized");
    
    // TODO: Implement endpoint logic
    const data = { status: "ok", endpoint: "ENDPOINT_NAME" };
    
    return new Response(
      JSON.stringify({ success: true, data }),
      { headers: { ...corsHeaders, "Content-Type": "application/json" } }
    );
  } catch (error: any) {
    return new Response(
      JSON.stringify({ success: false, error: error.message }),
      { status: 400, headers: { ...corsHeaders, "Content-Type": "application/json" } }
    );
  }
});
'@

Write-Host "Creating $($endpoints.Count) endpoints..." -ForegroundColor Cyan

foreach ($endpoint in $endpoints) {
    $dirPath = Join-Path $baseDir $endpoint
    $filePath = Join-Path $dirPath "index.ts"
    
    if (!(Test-Path $dirPath)) {
        New-Item -ItemType Directory -Path $dirPath -Force | Out-Null
    }
    
    $content = $simpleTemplate.Replace("ENDPOINT_NAME", $endpoint)
    Set-Content -Path $filePath -Value $content -Encoding UTF8
    
    Write-Host "Created: $endpoint" -ForegroundColor Green
}

Write-Host "`nDeploying all endpoints..." -ForegroundColor Cyan
Set-Location "C:\Users\raild\MEDICONNECT 13-11\riseup-squad18"

foreach ($endpoint in $endpoints) {
    Write-Host "Deploying $endpoint..." -ForegroundColor Yellow
    pnpx supabase functions deploy $endpoint --no-verify-jwt
}

Write-Host "`nDone! Check status with: pnpx supabase functions list" -ForegroundColor Green
