import type { PageMeta } from "./types";
import { getFleetRouteMeta } from "@/pages/fleet/config/resources";
import {
  RULE_ENGINE_CATEGORY_BASE_PATH,
  RULE_ENGINE_RESOURCES,
} from "@/pages/ruleEngine/config/resources";

const APP_TITLE = "EDR Freight Backoffice";
const APP_SUBTITLE = "Manage freight operations and platform settings";

const configurationRouteMeta = RULE_ENGINE_RESOURCES.filter(
  (r) => r.category === "configuration",
).map((resource) => ({
  prefix: `${RULE_ENGINE_CATEGORY_BASE_PATH.configuration}/${resource.slug}`,
  meta: {
    title: resource.label,
    subtitle: resource.subtitle,
  },
}));

const rulesRouteMeta = RULE_ENGINE_RESOURCES.filter((r) => r.category === "rules").map(
  (resource) => ({
    prefix: `${RULE_ENGINE_CATEGORY_BASE_PATH.rules}/${resource.slug}`,
    meta: {
      title: resource.label,
      subtitle: resource.subtitle,
    },
  }),
);

const ROUTE_META: Array<{ prefix: string; meta: PageMeta }> = [
  {
    prefix: "/dashboard/overview",
    meta: {
      title: "Overview",
      subtitle: "Dashboard summary and key metrics",
    },
  },
  {
    prefix: "/dashboard/profile",
    meta: {
      title: "My Profile",
      subtitle: "Manage your account and signature",
    },
  },
  {
    prefix: "/dashboard/payments",
    meta: {
      title: "Payments",
      subtitle: "View booking payment transactions",
    },
  },
  {
    prefix: "/dashboard/operations/first-mile",
    meta: {
      title: "First Mile",
      subtitle: "Assign vehicles to initial-leg pickups",
    },
  },
  {
    prefix: "/dashboard/operations/last-mile",
    meta: {
      title: "Last Mile",
      subtitle: "Assign vehicles to final-leg deliveries"
    }
  },
    {
    prefix: "/dashboard/warehouse-dashboard",
    meta: {
      title: "Warehouse Dashboard",
      subtitle: "Live overview of warehouse capacity and inventory lifecycle",
    },
  },
  {
    prefix: "/dashboard/warehouses/",
    meta: {
      title: "Warehouse detail",
      subtitle: "Yards, zones, and inventory for this warehouse",
    },
  },
  {
    prefix: "/dashboard/warehouses",
    meta: {
      title: "Warehouses",
      subtitle: "Manage warehouses, yards and zones",
    },
  },
  {
    prefix: "/dashboard/operations/train-scheduling-v2/",
    meta: {
      title: "Train schedule",
      subtitle: "Assign bookings, auto-pin wagons, finalize and dispatch",
    },
  },
  {
    prefix: "/dashboard/operations/train-scheduling-v2",
    meta: {
      title: "Train Schedules",
      subtitle: "Operational train scheduling with full allocation workflow",
    },
  },
  {
    prefix: "/dashboard/routes",
    meta: {
      title: "Routes",
      subtitle: "Manage route definitions built from freight yards",
    },
  },
  ...getFleetRouteMeta(),
  {
    prefix: "/dashboard/trains/",
    meta: {
      title: "Train detail",
      subtitle: "Manage fleet consist and wagon assignments",
    },
  },
  {
    prefix: "/dashboard/user-management/employees",
    meta: {
      title: "Employees",
      subtitle: "Manage employee accounts and assignments",
    },
  },
  {
    prefix: "/dashboard/user-management/permissions",
    meta: {
      title: "Permissions",
      subtitle: "Configure access permissions for roles and users",
    },
  },
  {
    prefix: "/dashboard/user-management/roles",
    meta: {
      title: "Roles",
      subtitle: "Manage roles and their permission sets",
    },
  },
  {
    prefix: "/dashboard/user-management",
    meta: {
      title: "User management",
      subtitle: "Organization structure, employees, roles, and permissions",
    },
  },
  {
    prefix: "/dashboard/file-settings",
    meta: {
      title: "File Settings",
      subtitle: "Configure file upload rules and document fields",
    },
  },
  {
    prefix: "/dashboard/dropdown-settings",
    meta: {
      title: "Dropdown Settings",
      subtitle: "Manage dropdown options used across the platform",
    },
  },
  {
    prefix: "/dashboard/configuration/train-scheduling-rules",
    meta: {
      title: "Train scheduling rules",
      subtitle: "Global limits for train length, weight, wagons, and 20ft container balance",
    },
  },
  {
    prefix: RULE_ENGINE_CATEGORY_BASE_PATH.configuration,
    meta: {
      title: "Configuration",
      subtitle: "Master data: cargo, containers, wagon types, services, yards, and shipping lines",
    },
  },
  ...configurationRouteMeta,
  {
    prefix: RULE_ENGINE_CATEGORY_BASE_PATH.rules,
    meta: {
      title: "Rules",
      subtitle: "Priority, weight limits, rates, and approval workflows",
    },
  },
  ...rulesRouteMeta,
  {
    prefix: "/dashboard/wagon-types",
    meta: {
      title: "Wagon Types",
      subtitle: "Manage wagon type capacity and supported load configuration",
    },
  },
  {
    prefix: "/dashboard/user1",
    meta: {
      title: "Demo User 1",
      subtitle: "Demo workspace",
    },
  },
  {
    prefix: "/dashboard/user2",
    meta: {
      title: "Demo User 2",
      subtitle: "Demo workspace",
    },
  },
];

export const getPageMeta = (pathname: string): PageMeta => {
  const normalized = pathname.toLowerCase();
  const sorted = [...ROUTE_META].sort((a, b) => b.prefix.length - a.prefix.length);
  const match = sorted.find(({ prefix }) => normalized.startsWith(prefix.toLowerCase()));

  if (match) {
    return match.meta;
  }

  return {
    title: APP_TITLE,
    subtitle: APP_SUBTITLE,
  };
};
