package queries

// ======================================================
// 👤 USER HEADER
// ======================================================
// ======================================================
// 👤 USER HEADER (mk_dfusr)
// ======================================================
const GetUserHeader = `
SELECT 
  id,
  username,
  is_active,
  COALESCE(full_name,'') AS full_name,
  COALESCE(email,'')     AS email,
  COALESCE(mobile,'')    AS mobile,
  COALESCE(address,'')   AS address,

  CASE
    WHEN password_hash IS NOT NULL AND password_hash <> '' THEN true
    ELSE false
  END AS has_password

FROM mk_dfusr
WHERE id = $1
`

// ======================================================
// 🔐 ROLES
// ======================================================
const GetUserRoles = `
SELECT r.code
FROM dfrole_usr ur
JOIN dfrole r ON r.id = ur.dfrole_id
WHERE ur.dfusr_id = $1
ORDER BY r.code
`

// ======================================================
// 🏢 DEPARTMENTS
// ======================================================
const GetUserDepartments = `
SELECT d.code, d.title
FROM dfusr_dprt ud
JOIN mk_dprt d ON d.id = ud.dprt_id
WHERE ud.dfusr_id = $1
  AND ud.is_active = true
ORDER BY d.code
`

// ======================================================
// 🌍 PIYASALAR
// ======================================================
const GetUserPiyasalar = `
SELECT
  COALESCE(p_code.code, p_title.code, up.piyasa_code) AS code,
  COALESCE(p_code.title, p_title.title, up.piyasa_code) AS title
FROM dfusr_piyasa up
LEFT JOIN mk_sales_piy p_code
  ON UPPER(translate(TRIM(p_code.code),  'çğıöşüÇĞİÖŞÜ', 'CGIOSUCGIOSU'))
   = UPPER(translate(TRIM(up.piyasa_code), 'çğıöşüÇĞİÖŞÜ', 'CGIOSUCGIOSU'))
LEFT JOIN mk_sales_piy p_title
  ON UPPER(translate(TRIM(p_title.title), 'çğıöşüÇĞİÖŞÜ', 'CGIOSUCGIOSU'))
   = UPPER(translate(TRIM(up.piyasa_code), 'çğıöşüÇĞİÖŞÜ', 'CGIOSUCGIOSU'))
WHERE up.dfusr_id = $1
  AND up.is_allowed = true
ORDER BY 1
`

// ======================================================
// 🧾 NEBIM USERS
// ======================================================
const GetUserNebim = `
SELECT n.username, n.user_group_code
FROM dfusr_nebim_user un
JOIN mk_nebim_user n ON n.id = un.mk_nebim_user_id
WHERE un.dfusr_id = $1
ORDER BY n.username
`

// ======================================================
// ✍️ UPDATE USER HEADER (SAFE)
// ======================================================
// ======================================================
// ✍️ UPDATE USER HEADER (mk_dfusr)
// ======================================================
const UpdateUserHeader = `
UPDATE mk_dfusr
SET
  username   = $2,
  is_active  = $3,
  full_name  = $4,
  email      = $5,
  mobile     = $6,
  address    = COALESCE($7, ''),
  updated_at = NOW()
WHERE id = $1
`

// ======================================================
// ➕ INSERT RELATIONS
// ======================================================

// ---------- ROLE
const InsertUserRole = `
INSERT INTO dfrole_usr (dfusr_id, dfrole_id)
SELECT $1, r.id
FROM dfrole r
WHERE r.code = $2
ON CONFLICT DO NOTHING
`

// ---------- DEPARTMENT
const InsertUserDepartment = `
INSERT INTO dfusr_dprt (dfusr_id, dprt_id, is_active)
SELECT $1, d.id, true
FROM mk_dprt d
WHERE d.code = $2
ON CONFLICT DO NOTHING
`

// ---------- PIYASA
const InsertUserPiyasa = `
INSERT INTO dfusr_piyasa (dfusr_id, piyasa_code, is_allowed)
VALUES ($1, $2, true)
ON CONFLICT DO NOTHING
`

// ---------- NEBIM USER
const InsertUserNebim = `
INSERT INTO dfusr_nebim_user (dfusr_id, mk_nebim_user_id)
SELECT $1, n.id
FROM mk_nebim_user n
WHERE n.username = $2
ON CONFLICT DO NOTHING
`

// ======================================================
// 📚 LOOKUPS
// ======================================================
const GetRoleLookup = `
SELECT code AS value, code AS label
FROM dfrole
ORDER BY code
`

const GetDepartmentLookup = `
SELECT code AS value, title AS label
FROM mk_dprt
WHERE is_active = true
ORDER BY code
`

const GetPiyasaLookup = `
SELECT code AS value, title AS label
FROM mk_sales_piy
WHERE is_active = true
ORDER BY code
`

const GetNebimUserLookup = `
SELECT 
  username AS value,
  username || ' (' || user_group_code || ')' AS label,
  user_group_code
FROM mk_nebim_user
WHERE is_active = true
ORDER BY username
`

// ======================================================
// 📦 ORDER — NEW NUMBER
// ======================================================

const GetNextOrderNumber = `
SELECT
	COALESCE(MAX(order_number), 0) + 1
FROM orders
WHERE order_year = EXTRACT(YEAR FROM NOW())
`

// ======================================================
// 🔐 ROLES (ADMIN LIST)
// ======================================================

const GetRoles = `
SELECT
	id,
	code,
	title
FROM dfrole
ORDER BY code
`

// ======================================================
// 🏢 DEPARTMENTS (ADMIN LIST)
// ======================================================

const GetDepartments = `
SELECT
	id,
	code,
	title
FROM mk_dprt
WHERE is_active = true
ORDER BY code
`

// ======================================================
// 🌍 PIYASALAR (ADMIN LIST)
// ======================================================

const GetPiyasalar = `
SELECT
	code,
	title
FROM mk_sales_piy
WHERE is_active = true
ORDER BY code
`

// ======================================================
// 🔗 ROLE → DEPARTMENT
// ======================================================

const DeleteRoleDepartments = `
DELETE FROM dfrole_dprt
WHERE dfrole_id = $1
`

const InsertRoleDepartment = `
INSERT INTO dfrole_dprt (
	dfrole_id,
	dprt_id,
	is_allowed
)
SELECT
	$1,
	d.id,
	true
FROM mk_dprt d
WHERE d.code = $2
ON CONFLICT DO NOTHING
`

// ======================================================
// 🔗 ROLE → PIYASA
// ======================================================

const DeleteRolePiyasalar = `
DELETE FROM dfrole_piyasa
WHERE dfrole_id = $1
`

const InsertRolePiyasa = `
INSERT INTO dfrole_piyasa (
	dfrole_id,
	piyasa_code,
	is_allowed
)
VALUES ($1,$2,true)
ON CONFLICT DO NOTHING
`

// ======================================================
// 👤 USER → ROLE
// ======================================================

const DeleteUserRoles = `
DELETE FROM dfrole_usr
WHERE dfusr_id = $1
`

// ======================================================
// 🏢 DEPARTMENTS (ADMIN PERM PAGE SELECT) -> { code, title }
// ======================================================
const GetDepartmentsForPermissionSelect = `
SELECT
  code AS id,
  title AS title
FROM mk_dprt
WHERE is_active = true
ORDER BY title
`

// ======================================================
// 🔐 ROLES (ADMIN PERM PAGE SELECT)  -> { id, title }
// ======================================================
const GetRolesForPermissionSelect = `
SELECT
  id::text AS id,
  COALESCE(NULLIF(title,''), code) AS title
FROM dfrole
ORDER BY COALESCE(NULLIF(title,''), code)
`
const GetUserLookupForPermission = `
SELECT
  id::text AS id,
  username || ' - ' || full_name AS title
FROM mk_dfusr
WHERE is_active = true
ORDER BY username
`
