"use server";

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

// TODO(migration): the time-tracking admin RPCs (get_time_tracking_workers,
// create_time_worker, reset_time_worker_pin, update_time_worker,
// delete_time_worker, create_time_task, update_time_task,
// delete_time_task, get_worker_time_logs, update_worker_time_log,
// delete_worker_time_log, get_time_tracking_summary,
// get_time_tracking_settings, update_time_tracking_settings,
// get_time_tracking_notification_log, check_and_notify_overtime) and
// the underlying tables (`time_workers`, `time_tasks`, `time_logs`,
// `time_tracking_settings`, `time_tracking_notification_log`) were
// not carried over into the SaaS rebuild's `db/schema/`. The actions
// below return empty results. To bring time tracking back, add
// the tables to `db/schema/` and re-implement against Drizzle. See
// `actions/route-trace/lots.ts` for the same pattern.

// ── 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[]> {
  // Time tracking tables not in SaaS rebuild — return empty list.
  return [];
}

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" };
  return { success: false, error: "Time tracking is not configured" };
}

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" };
  return { success: false, error: "Time tracking is not configured" };
}

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" };
  return { success: false, error: "Time tracking is not configured" };
}

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

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

export async function getTimeTrackingTasks(_brandId: string, _activeOnly = false): Promise<TimeTask[]> {
  return [];
}

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" };
  return { success: false, error: "Time tracking is not configured" };
}

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" };
  return { success: false, error: "Time tracking is not configured" };
}

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

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

export async function getWorkerTimeLogs(
  _brandId: string,
  _options: {
    workerId?: string;
    taskId?: string;
    start?: string;
    end?: string;
    limit?: number;
    offset?: number;
  } = {}
): Promise<TimeLog[]> {
  return [];
}

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" };
  return { success: false, error: "Time tracking is not configured" };
}

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

export async function getTimeTrackingSummary(
  _brandId: string,
  _start: string,
  _end: string
): Promise<TimeSummary> {
  return { by_worker: [], by_task: [], totals: { entry_count: 0, total_hours: 0, open_count: 0 } };
}

// ── 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> {
  // Real RPC not in SaaS rebuild.
  return null;
}

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" };
  return { success: false, error: "Time tracking is not configured" };
}

// ── 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[]> {
  return [];
}