-- name: ListActiveEmployees :many
SELECT 
  de.id, 
  de.firstname, 
  de.surname, 
  de."contractStart", 
  de."contractEnd", 
  de."contractHoursWeek", 
  de."contractSalaryHour",
  de."contractTypeName",
  e.id as "dlId",
  e."personTransportCertificate"
FROM "DyflexisEmployee" as de
INNER JOIN "Employee" as e ON de.id::text = e."personnelNumber"
WHERE de."contractStart" < CURRENT_DATE AND de."contractEnd" > (CURRENT_DATE - INTERVAL '2 months');

-- name: GetWorkSummaryForEmployeeInPeriod :one
WITH wt AS (
  SELECT 
    COUNT(*) as days,
    COALESCE(sum(duration), 0)::bigint as minutes
  FROM "WorkTime"
  WHERE "userId" = @employee_id AND "startDate" > @start_date AND "startDate" < @end_date
)
SELECT
  wt.days,
  (minutes / 60.0)::float as hours,
  wt.minutes
FROM wt;

-- name: ListPlannedTimesForEmployeeInPeriod :many
SELECT 
  DISTINCT ON (day)
  "startDate"::DATE as day,
  "startDate",
  "endDate",
  department,
  pause,
  duration
FROM "PlannedTime"
WHERE 
  "userId" = @employee_id AND 
  "startDate" > @start_date AND 
  "startDate" < @end_date AND 
  department <> 'Fortbildung' AND
  note IS NULL
ORDER BY day, "startDate" ASC;

-- name: ListWorkTimesForEmployeeInPeriod :many
SELECT * FROM "WorkTime"
WHERE "userId" = @employee_id AND "startDate" > @start_date AND "startDate" < @end_date; 

-- name: GetSicknessSummaryForEmployeeInPeriod :one
WITH pt AS (
  SELECT 
    COUNT(*) as days,
    COALESCE(sum(duration), 0)::bigint as minutes
  FROM "PlannedTime"
  WHERE "userId" = @employee_id AND "startDate" > @start_date AND "startDate" < @end_date AND note = 'sick'
)
SELECT
  pt.days,
  (minutes / 60)::float as hours,
  pt.minutes
FROM pt;

-- name: GetEmployeeDyflexis :one
SELECT * FROM "DyflexisEmployee"
WHERE 
  id = $1 AND 
  "contractStart" < CURRENT_DATE AND 
  "contractEnd" > (CURRENT_DATE - INTERVAL '2 months')
LIMIT 1;

-- name: FetchDrivenToursInInterval :many
SELECT * FROM "Tour"
WHERE "check" <> 3 
AND "deletedAt" IS NULL
AND (
  "patientId" IS NULL
  OR "patientId" NOT IN (
    'b7424477-a4b2-42f7-b7e7-a4bef24f6979',
    '3554e1a9-9c07-45e5-b53c-692a77e4fa9a'
  )
)
-- ID's for Pause and Feierabend respectively
AND (
  "carName" IS NULL
  OR "carName" NOT IN (
    'Storno',
    'Abgabe',
    'Löschen'
  )
)
AND "startDate" >= @start_date
AND "startDate" <= @end_date
AND ("driverId" = @employee_id OR "codriverId" = @employee_id)
ORDER BY "startDate" DESC;

-- name: CountTourengeldAmountInPeriod :one
WITH ToursInPeriod AS (
  SELECT 
    (("startDate" AT TIME ZONE 'UTC') AT TIME ZONE 'Europe/Berlin')::DATE as day,
    "operationId",
    "occupiedKm",
    CASE
      WHEN "occupiedKm" <= 30 THEN 1.0
      ELSE ROUND("occupiedKm" / 30.0)
    END AS tour_value,
    CASE
      WHEN EXTRACT(ISODOW FROM "startDate") = 6 AND "occupiedKm" <= 30 THEN 0.5
      ELSE 0.0
    END AS weekend_bonus,
    "infectionName",
    CASE
      WHEN "infectionName" = 'Covid-19' THEN 0.5
      ELSE 0.0
    END AS infection_bonus
  FROM
    "Tour"
  WHERE
    "check" <> 3 
    AND "deletedAt" IS NULL
    AND (
      "patientId" IS NULL
      OR "patientId" NOT IN (
        'b7424477-a4b2-42f7-b7e7-a4bef24f6979',
        '3554e1a9-9c07-45e5-b53c-692a77e4fa9a'
      )
    )
    -- ID's for Pause and Feierabend respectively
    AND (
      "carName" IS NULL
      OR "carName" NOT IN (
        'Storno',
        'Abgabe',
        'Löschen'
      )
      AND "carName" LIKE '%KTW%'
    )
    AND "startDate" >= @start_date
    AND "startDate" <= @end_date
    AND ("driverId" ILIKE @employee_id OR "codriverId" ILIKE @employee_id)
  ORDER BY 
    "day"
),
DailyCounts AS (
  SELECT
    day,
    SUM(tour_value) + SUM(weekend_bonus) + SUM(infection_bonus) AS tour_amount
  FROM
    ToursInPeriod
  GROUP BY
    day
),
HighActivityDays AS (
  SELECT 
    day, 
    GREATEST(0, tour_amount - 6) AS tourengeld 
  FROM 
    DailyCounts
)
SELECT 
  SUM(tourengeld)::float as tourengeld
FROM
  HighActivityDays;

-- name: GetEmployeeByName :one
SELECT 
  dy.id as dyflexis_id, 
  dl.id as dispolive_id, 
  dy."contractSalaryHour" 
FROM "DyflexisEmployee" as dy
INNER JOIN
  "Employee" as dl ON dy.id::text = dl."personnelNumber" 
WHERE 
  TRIM(dy.firstname) = @firstname AND 
  TRIM(dy.surname) = @lastname AND
  "contractStart" < CURRENT_DATE AND 
  "contractEnd" > (CURRENT_DATE - INTERVAL '2 months')
ORDER BY dy."contractEnd" DESC;
