"use server";

import { getAdminUser } from "@/lib/admin-permissions";
import { svcHeaders } from "@/lib/svc-headers";

const supabaseUrl = process.env.NEXT_PUBLIC_API_URL!;
const supabaseKey = process.env.POSTGREST_SERVICE_KEY!;

function rpcBody(body: Record<string, unknown>) {
  return JSON.stringify(body);
}

// ── Types ─────────────────────────────────────────────────────────────────────

export type TimeWorker = {
  id: string;
  brand_id: string;
  name: string;
  role: string;
  lang: string;
  pin: string;
  active: boolean;
  last_used_at: string | null;
  created_at: string;
  worker_number: number | null;
};

export type TimeTask = {
  id: string;
  name: string;
  name_es: string | null;
  unit: string;
  active: boolean;
  sort_order: number;
};

export type TimeLog = {
  id: string;
  worker_id: string;
  worker_name: string;
  task_id: string | null;
  task_name: string;
  clock_in: string;
  clock_out: string | null;
  lunch_break_minutes: number;
  notes: string | null;
  submitted_via: string;
  total_minutes: number;
  created_at: string;
};

export type TimeSummary = {
  by_worker: { id: string; name: string; entry_count: number; total_hours: number }[];
  by_task: { id: string; name: string; name_es: string | null; entry_count: number; total_hours: number }[];
  totals: { entry_count: number; total_hours: number; open_count: number };
};

// ── Workers ───────────────────────────────────────────────────────────────────

export async function getTimeTrackingWorkers(brandId: string): Promise<TimeWorker[]> {
  const res = await fetch(
    `${supabaseUrl}/rest/v1/rpc/get_time_tracking_workers`,
    {
      method: "POST",
      headers: { ...svcHeaders(supabaseKey), "Content-Type": "application/json" },
      body: rpcBody({ p_brand_id: brandId }),
    }
  );
  if (!res.ok) return [];
  const data = await res.json();
  return (data?.workers ?? []).map((w: Record<string, unknown>) => ({
    id: w.id as string,
    brand_id: w.brand_id as string,
    name: w.name as string,
    role: w.role as string,
    lang: w.lang as string,
    pin: w.pin as string,
    active: w.active as boolean,
    last_used_at: w.last_used_at as string | null,
    created_at: w.created_at as string,
    worker_number: w.worker_number as number | null,
  }));
}

export async function createTimeWorker(
  brandId: string,
  name: string,
  role = "worker",
  lang = "en"
): Promise<{ success: boolean; worker?: TimeWorker; pin?: string; error?: string }> {
  const adminUser = await getAdminUser();
  if (!adminUser) return { success: false, error: "Not authenticated" };

  const res = await fetch(
    `${supabaseUrl}/rest/v1/rpc/create_time_worker`,
    {
      method: "POST",
      headers: { ...svcHeaders(supabaseKey), "Content-Type": "application/json" },
      body: rpcBody({ p_brand_id: brandId, p_name: name, p_role: role, p_lang: lang }),
    }
  );
  const data = await res.json();
  if (!res.ok || !data?.success) return { success: false, error: data?.error ?? "Failed" };
  return { success: true, worker: data.worker, pin: data.pin };
}

export async function resetTimeWorkerPin(workerId: string): Promise<{ success: boolean; pin?: string; error?: string }> {
  const adminUser = await getAdminUser();
  if (!adminUser) return { success: false, error: "Not authenticated" };

  const res = await fetch(
    `${supabaseUrl}/rest/v1/rpc/reset_time_worker_pin`,
    {
      method: "POST",
      headers: { ...svcHeaders(supabaseKey), "Content-Type": "application/json" },
      body: rpcBody({ p_worker_id: workerId }),
    }
  );
  const data = await res.json();
  if (!res.ok || !data?.success) return { success: false, error: data?.error ?? "Failed" };
  return { success: true, pin: data.pin };
}

export async function updateTimeWorker(
  workerId: string,
  name: string,
  role: string,
  lang: string,
  active: boolean
): Promise<{ success: boolean; error?: string }> {
  const adminUser = await getAdminUser();
  if (!adminUser) return { success: false, error: "Not authenticated" };

  const res = await fetch(
    `${supabaseUrl}/rest/v1/rpc/update_time_worker`,
    {
      method: "POST",
      headers: { ...svcHeaders(supabaseKey), "Content-Type": "application/json" },
      body: rpcBody({ p_worker_id: workerId, p_name: name, p_role: role, p_lang: lang, p_active: active }),
    }
  );
  const data = await res.json();
  if (!res.ok || !data?.success) return { success: false, error: data?.error ?? "Failed" };
  return { success: true };
}

export async function deleteTimeWorker(workerId: string): Promise<{ success: boolean; error?: string }> {
  const adminUser = await getAdminUser();
  if (!adminUser) return { success: false, error: "Not authenticated" };

  const res = await fetch(
    `${supabaseUrl}/rest/v1/rpc/delete_time_worker`,
    {
      method: "POST",
      headers: { ...svcHeaders(supabaseKey), "Content-Type": "application/json" },
      body: rpcBody({ p_worker_id: workerId }),
    }
  );
  const data = await res.json();
  if (!res.ok || !data?.success) return { success: false, error: data?.error ?? "Failed" };
  return { success: true };
}

// ── Tasks ────────────────────────────────────────────────────────────────────

export async function getTimeTrackingTasks(brandId: string, activeOnly = false): Promise<TimeTask[]> {
  const res = await fetch(
    `${supabaseUrl}/rest/v1/rpc/get_time_tracking_tasks`,
    {
      method: "POST",
      headers: { ...svcHeaders(supabaseKey), "Content-Type": "application/json" },
      body: rpcBody({ p_brand_id: brandId, p_active_only: activeOnly }),
    }
  );
  if (!res.ok) return [];
  const data = await res.json();
  return data?.tasks ?? [];
}

export async function createTimeTask(
  brandId: string,
  name: string,
  nameEs: string | null = null,
  unit = "hours",
  sortOrder = 0
): Promise<{ success: boolean; id?: string; error?: string }> {
  const adminUser = await getAdminUser();
  if (!adminUser) return { success: false, error: "Not authenticated" };

  const res = await fetch(
    `${supabaseUrl}/rest/v1/rpc/create_time_task`,
    {
      method: "POST",
      headers: { ...svcHeaders(supabaseKey), "Content-Type": "application/json" },
      body: rpcBody({ p_brand_id: brandId, p_name: name, p_name_es: nameEs, p_unit: unit, p_sort_order: sortOrder }),
    }
  );
  const data = await res.json();
  if (!res.ok || !data?.success) return { success: false, error: data?.error ?? "Failed" };
  return { success: true, id: data.id };
}

export async function updateTimeTask(
  taskId: string,
  name: string,
  nameEs: string,
  unit: string,
  active: boolean,
  sortOrder: number
): Promise<{ success: boolean; error?: string }> {
  const adminUser = await getAdminUser();
  if (!adminUser) return { success: false, error: "Not authenticated" };

  const res = await fetch(
    `${supabaseUrl}/rest/v1/rpc/update_time_task`,
    {
      method: "POST",
      headers: { ...svcHeaders(supabaseKey), "Content-Type": "application/json" },
      body: rpcBody({ p_task_id: taskId, p_name: name, p_name_es: nameEs, p_unit: unit, p_active: active, p_sort_order: sortOrder }),
    }
  );
  const data = await res.json();
  if (!res.ok || !data?.success) return { success: false, error: data?.error ?? "Failed" };
  return { success: true };
}

export async function deleteTimeTask(taskId: string): Promise<{ success: boolean; error?: string }> {
  const adminUser = await getAdminUser();
  if (!adminUser) return { success: false, error: "Not authenticated" };

  const res = await fetch(
    `${supabaseUrl}/rest/v1/rpc/delete_time_task`,
    {
      method: "POST",
      headers: { ...svcHeaders(supabaseKey), "Content-Type": "application/json" },
      body: rpcBody({ p_task_id: taskId }),
    }
  );
  const data = await res.json();
  if (!res.ok || !data?.success) return { success: false, error: data?.error ?? "Failed" };
  return { success: true };
}

// ── Time Logs ─────────────────────────────────────────────────────────────────

export async function getWorkerTimeLogs(
  brandId: string,
  options: {
    workerId?: string;
    taskId?: string;
    start?: string;
    end?: string;
    limit?: number;
    offset?: number;
  } = {}
): Promise<TimeLog[]> {
  const res = await fetch(
    `${supabaseUrl}/rest/v1/rpc/get_worker_time_logs`,
    {
      method: "POST",
      headers: { ...svcHeaders(supabaseKey), "Content-Type": "application/json" },
      body: rpcBody({
        p_brand_id: brandId,
        p_worker_id: options.workerId ?? null,
        p_task_id: options.taskId ?? null,
        p_start: options.start ?? null,
        p_end: options.end ?? null,
        p_limit: options.limit ?? 200,
        p_offset: options.offset ?? 0,
      }),
    }
  );
  if (!res.ok) return [];
  const data = await res.json();
  return data?.logs ?? [];
}

export async function updateWorkerTimeLog(
  logId: string,
  taskName: string,
  clockIn: string,
  clockOut: string | null,
  lunchMinutes: number,
  notes: string | null
): Promise<{ success: boolean; error?: string }> {
  const adminUser = await getAdminUser();
  if (!adminUser) return { success: false, error: "Not authenticated" };

  const res = await fetch(
    `${supabaseUrl}/rest/v1/rpc/update_worker_time_log`,
    {
      method: "POST",
      headers: { ...svcHeaders(supabaseKey), "Content-Type": "application/json" },
      body: rpcBody({
        p_log_id: logId,
        p_task_name: taskName,
        p_clock_in: clockIn,
        p_clock_out: clockOut,
        p_lunch_minutes: lunchMinutes,
        p_notes: notes,
      }),
    }
  );
  const data = await res.json();
  if (!res.ok || !data?.success) return { success: false, error: data?.error ?? "Failed" };
  return { success: true };
}

export async function deleteWorkerTimeLog(logId: string): Promise<{ success: boolean; error?: string }> {
  const adminUser = await getAdminUser();
  if (!adminUser) return { success: false, error: "Not authenticated" };

  const res = await fetch(
    `${supabaseUrl}/rest/v1/rpc/delete_worker_time_log`,
    {
      method: "POST",
      headers: { ...svcHeaders(supabaseKey), "Content-Type": "application/json" },
      body: rpcBody({ p_log_id: logId }),
    }
  );
  const data = await res.json();
  if (!res.ok || !data?.success) return { success: false, error: data?.error ?? "Failed" };
  return { success: true };
}

export async function getTimeTrackingSummary(
  brandId: string,
  start: string,
  end: string
): Promise<TimeSummary> {
  const res = await fetch(
    `${supabaseUrl}/rest/v1/rpc/get_time_tracking_summary`,
    {
      method: "POST",
      headers: { ...svcHeaders(supabaseKey), "Content-Type": "application/json" },
      body: rpcBody({ p_brand_id: brandId, p_start: start, p_end: end }),
    }
  );
  if (!res.ok) return { by_worker: [], by_task: [], totals: { entry_count: 0, total_hours: 0, open_count: 0 } };
  return res.json();
}

// ── Time Tracking Settings ─────────────────────────────────────────────────────

export type TimeTrackingSettings = {
  id: string;
  brand_id: string;
  pay_period_start_day: number;
  pay_period_length_days: number;
  daily_overtime_threshold: number;
  weekly_overtime_threshold: number;
  overtime_multiplier: number;
  overtime_notifications: boolean;
  notification_emails: string[];
  notification_sms_numbers: string[];
  enable_daily_alerts: boolean;
  enable_weekly_alerts: boolean;
  daily_alert_threshold: number;
  weekly_alert_threshold: number;
  send_end_of_period_summary: boolean;
  brand_name: string;
};

export async function getTimeTrackingSettings(brandId: string): Promise<TimeTrackingSettings | null> {
  const res = await fetch(
    `${supabaseUrl}/rest/v1/rpc/get_time_tracking_settings`,
    {
      method: "POST",
      headers: { ...svcHeaders(supabaseKey), "Content-Type": "application/json" },
      body: rpcBody({ p_brand_id: brandId }),
    }
  );
  if (!res.ok) return null;
  const data = await res.json();
  return data;
}

export async function updateTimeTrackingSettings(
  brandId: string,
  settings: {
    pay_period_start_day: number;
    pay_period_length_days: number;
    daily_overtime_threshold: number;
    weekly_overtime_threshold: number;
    overtime_multiplier: number;
    overtime_notifications: boolean;
    notification_emails?: string[];
    notification_sms_numbers?: string[];
    enable_daily_alerts?: boolean;
    enable_weekly_alerts?: boolean;
    daily_alert_threshold?: number;
    weekly_alert_threshold?: number;
    send_end_of_period_summary?: boolean;
    brand_name?: string;
  }
): Promise<{ success: boolean; error?: string }> {
  const adminUser = await getAdminUser();
  if (!adminUser) return { success: false, error: "Not authenticated" };

  const res = await fetch(
    `${supabaseUrl}/rest/v1/rpc/update_time_tracking_settings`,
    {
      method: "POST",
      headers: { ...svcHeaders(supabaseKey), "Content-Type": "application/json" },
      body: rpcBody({
        p_brand_id: brandId,
        p_pay_period_start_day: settings.pay_period_start_day,
        p_pay_period_length_days: settings.pay_period_length_days,
        p_daily_threshold: settings.daily_overtime_threshold,
        p_weekly_threshold: settings.weekly_overtime_threshold,
        p_overtime_multiplier: settings.overtime_multiplier,
        p_overtime_notifications: settings.overtime_notifications,
        p_notification_emails: settings.notification_emails ?? null,
        p_notification_sms_numbers: settings.notification_sms_numbers ?? null,
        p_enable_daily_alerts: settings.enable_daily_alerts ?? null,
        p_enable_weekly_alerts: settings.enable_weekly_alerts ?? null,
        p_daily_alert_threshold: settings.daily_alert_threshold ?? null,
        p_weekly_alert_threshold: settings.weekly_alert_threshold ?? null,
        p_send_end_of_period_summary: settings.send_end_of_period_summary ?? null,
        p_brand_name: settings.brand_name ?? null,
      }),
    }
  );
  const data = await res.json();
  if (!res.ok || !data?.success) return { success: false, error: data?.error ?? "Failed" };
  return { success: true };
}

// ── Notification Log ─────────────────────────────────────────────────────────

export type NotificationLogEntry = {
  id: string;
  worker_id: string | null;
  worker_name: string | null;
  trigger_type: string;
  threshold_hours: number | null;
  actual_hours: number | null;
  emails_sent: string[];
  sms_numbers_sent: string[];
  email_sent: boolean;
  sms_sent: boolean;
  error_message: string | null;
  created_at: string;
};

export async function getTimeTrackingNotificationLog(
  brandId: string,
  limit = 100
): Promise<NotificationLogEntry[]> {
  const res = await fetch(
    `${supabaseUrl}/rest/v1/rpc/get_time_tracking_notification_log`,
    {
      method: "POST",
      headers: { ...svcHeaders(supabaseKey), "Content-Type": "application/json" },
      body: rpcBody({ p_brand_id: brandId, p_limit: limit }),
    }
  );
  if (!res.ok) return [];
  const data = await res.json();
  return data ?? [];
}
