-- Split single verification_photo_url into separate check-in and check-out columns.
-- Also add check_out_justification for off-site checkout.

-- Add new columns
DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_name = 'attendance_logs' AND column_name = 'check_in_verification_photo_url'
  ) THEN
    ALTER TABLE attendance_logs ADD COLUMN check_in_verification_photo_url TEXT;
  END IF;
END $$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_name = 'attendance_logs' AND column_name = 'check_out_verification_photo_url'
  ) THEN
    ALTER TABLE attendance_logs ADD COLUMN check_out_verification_photo_url TEXT;
  END IF;
END $$;

DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM information_schema.columns
    WHERE table_name = 'attendance_logs' AND column_name = 'check_out_justification'
  ) THEN
    ALTER TABLE attendance_logs ADD COLUMN check_out_justification TEXT;
  END IF;
END $$;

-- Migrate existing data: copy verification_photo_url → check_in_verification_photo_url
-- (the old column stored the last verification photo, which was overwritten by checkout;
--  for rows that were only checked in, this is the check-in photo)
UPDATE attendance_logs
SET check_in_verification_photo_url = verification_photo_url
WHERE verification_photo_url IS NOT NULL
  AND check_in_verification_photo_url IS NULL;

-- Update checkout RPC to accept optional justification
CREATE OR REPLACE FUNCTION public.attendance_check_out(
  p_attendance_id uuid,
  p_lat double precision,
  p_lng double precision,
  p_justification text DEFAULT NULL
) RETURNS void
LANGUAGE plpgsql SECURITY DEFINER
AS $$
DECLARE
  v_log attendance_logs%ROWTYPE;
BEGIN
  SELECT * INTO v_log FROM attendance_logs WHERE id = p_attendance_id;
  IF NOT FOUND THEN
    RAISE EXCEPTION 'Attendance log not found';
  END IF;
  IF v_log.user_id != auth.uid() THEN
    RAISE EXCEPTION 'Not your attendance log';
  END IF;
  IF v_log.check_out_at IS NOT NULL THEN
    RAISE EXCEPTION 'Already checked out';
  END IF;

  UPDATE attendance_logs
  SET check_out_at = now(),
      check_out_lat = p_lat,
      check_out_lng = p_lng,
      check_out_justification = p_justification
  WHERE id = p_attendance_id;
END;
$$;
