import { ROLE_NAMES, type RoleName } from '@/shared/constants/roles';
import { PRODUCT_CAMPUS_SEED_ROWS } from '@/shared/constants/campuses';
import {
  USER_NAME_PREFIXES,
  type UserNamePrefix,
} from '@/shared/constants/users';

/**
 * RBAC seed fixtures (Workstream 4): one company, two schools, the six product
 * campuses, campus-role users, and exactly one loginable user per
 * stored role. Single source shared by the admin-user seeder (creates the
 * users), the user-roles seeder (assigns roles), and the rbac-fixtures seeder
 * (org/school/campus links). Pre-launch — reset the DB and reseed.
 */

export const SEED_ORGANIZATION_ID = 'b1a7c0de-0000-4000-8000-000000000001';
export const SEED_ORGANIZATION_NAME = 'Demo Academy';

/**
 * Two schools under the primary org (Organization → School → Campus). School 1
 * (North) owns the first three campuses (incl. the `tigers` campus the fixture
 * staff are on); School 2 (South) owns the rest — used to prove hard isolation
 * between schools. Every campus belongs to exactly one school.
 */
export const SEED_SCHOOL_ID = 'b1a7c0de-0000-4000-8000-000000000031';
export const SEED_SCHOOL_NAME = 'Demo Academy North';
export const SEED_SCHOOL_2_ID = 'b1a7c0de-0000-4000-8000-000000000032';
export const SEED_SCHOOL_2_NAME = 'Demo Academy South';

/** The campus the fixture campus-role users are assigned to (the seeded `tigers` campus). */
export const SEED_CAMPUS_ID = PRODUCT_CAMPUS_SEED_ROWS[0].id;

/**
 * A seeded class (the `tigers` campus homeroom) for the Class tier. The teacher/
 * support_staff fixtures are assigned to it, the student is enrolled, and the
 * guardian is linked to that student.
 */
export const SEED_CLASS_ID = 'b1a7c0de-0000-4000-8000-000000000041';
export const SEED_CLASS_NAME = 'Tigers Homeroom 1';

/** Campus → school assignment (campus belongs to exactly one school). */
export const SEED_SCHOOL_CAMPUS_IDS: Readonly<Record<string, readonly string[]>> =
  Object.freeze({
    [SEED_SCHOOL_ID]: PRODUCT_CAMPUS_SEED_ROWS.slice(0, 3).map((c) => c.id),
    [SEED_SCHOOL_2_ID]: PRODUCT_CAMPUS_SEED_ROWS.slice(3).map((c) => c.id),
  });

export interface SeedFixtureUser {
  readonly id: string;
  readonly email: string;
  /** Honorific title; the UI renders it before the name (not baked into it). */
  readonly namePrefix?: UserNamePrefix;
  readonly firstName: string;
  readonly lastName: string;
  readonly phoneNumber?: string;
  readonly role: RoleName;
  /** Uses `SEED_ADMIN_PASSWORD` (system roles) vs `SEED_USER_PASSWORD`. */
  readonly admin: boolean;
  /** Gets `organizationId` (org/school/campus/external roles; not the system roles). */
  readonly organization: boolean;
  /** Gets `schoolId` = `SEED_SCHOOL_ID` (school + campus + external roles in School 1). */
  readonly school: boolean;
  /** Gets `campusId` (campus + external roles). */
  readonly campus: boolean;
}

export const SEED_FIXTURE_USERS: readonly SeedFixtureUser[] = [
  { id: 'b1a7c0de-0000-4000-8000-000000000010', email: 'admin@flatlogic.com', namePrefix: USER_NAME_PREFIXES.MR, firstName: 'Alex', lastName: 'Morgan', role: ROLE_NAMES.SUPER_ADMIN, admin: true, organization: false, school: false, campus: false },
  { id: 'b1a7c0de-0000-4000-8000-000000000011', email: 'system_admin@flatlogic.com', namePrefix: USER_NAME_PREFIXES.MS, firstName: 'Jordan', lastName: 'Chen', role: ROLE_NAMES.SYSTEM_ADMIN, admin: true, organization: false, school: false, campus: false },
  { id: 'b1a7c0de-0000-4000-8000-000000000012', email: 'owner@flatlogic.com', namePrefix: USER_NAME_PREFIXES.MRS, firstName: 'Patricia', lastName: 'Hayes', role: ROLE_NAMES.OWNER, admin: false, organization: true, school: false, campus: false },
  { id: 'b1a7c0de-0000-4000-8000-000000000013', email: 'superintendent@flatlogic.com', namePrefix: USER_NAME_PREFIXES.DR, firstName: 'Michael', lastName: 'Torres', role: ROLE_NAMES.SUPERINTENDENT, admin: false, organization: true, school: false, campus: false },
  { id: 'b1a7c0de-0000-4000-8000-000000000021', email: 'principal@flatlogic.com', namePrefix: USER_NAME_PREFIXES.DR, firstName: 'Karen', lastName: 'Mitchell', role: ROLE_NAMES.PRINCIPAL, admin: false, organization: true, school: true, campus: false },
  { id: 'b1a7c0de-0000-4000-8000-000000000022', email: 'registrar@flatlogic.com', namePrefix: USER_NAME_PREFIXES.MS, firstName: 'Nicole', lastName: 'Adams', role: ROLE_NAMES.REGISTRAR, admin: false, organization: true, school: true, campus: false },
  { id: 'b1a7c0de-0000-4000-8000-000000000014', email: 'director@flatlogic.com', namePrefix: USER_NAME_PREFIXES.DR, firstName: 'Sarah', lastName: 'Williams', role: ROLE_NAMES.DIRECTOR, admin: false, organization: true, school: true, campus: true },
  { id: 'b1a7c0de-0000-4000-8000-000000000015', email: 'office_manager@flatlogic.com', namePrefix: USER_NAME_PREFIXES.MS, firstName: 'Lisa', lastName: 'Park', role: ROLE_NAMES.OFFICE_MANAGER, admin: false, organization: true, school: true, campus: true },
  { id: 'b1a7c0de-0000-4000-8000-000000000016', email: 'teacher@flatlogic.com', namePrefix: USER_NAME_PREFIXES.MRS, firstName: 'Emily', lastName: 'Johnson', role: ROLE_NAMES.TEACHER, admin: false, organization: true, school: true, campus: true },
  { id: 'b1a7c0de-0000-4000-8000-000000000017', email: 'support_staff@flatlogic.com', namePrefix: USER_NAME_PREFIXES.MR, firstName: 'Marcus', lastName: 'Davis', role: ROLE_NAMES.SUPPORT_STAFF, admin: false, organization: true, school: true, campus: true },
  { id: 'b1a7c0de-0000-4000-8000-000000000018', email: 'student@flatlogic.com', firstName: 'Emma', lastName: 'Clark', role: ROLE_NAMES.STUDENT, admin: false, organization: true, school: true, campus: true },
  { id: 'b1a7c0de-0000-4000-8000-000000000019', email: 'guardian@flatlogic.com', namePrefix: USER_NAME_PREFIXES.MR, firstName: 'Robert', lastName: 'Clark', role: ROLE_NAMES.GUARDIAN, admin: false, organization: true, school: true, campus: true },
];

/**
 * A **second tenant** used to prove cross-tenant isolation and to support
 * manual QA across tenant roles. It has the same non-system user topology as
 * the primary tenant: owner, superintendent, school roles, campus staff,
 * student, and guardian. System roles stay global and are not tenant-owned.
 */
export const SEED_ORGANIZATION_2_ID = 'b1a7c0de-0000-4000-8000-000000000002';
export const SEED_ORGANIZATION_2_NAME = 'Rival Academy';

export const SEED_SECONDARY_SCHOOL_ID = 'b1a7c0de-0000-4000-8000-000000000061';
export const SEED_SECONDARY_SCHOOL_NAME = 'Rival Academy North';
export const SEED_SECONDARY_CAMPUS_ID = 'b1a7c0de-0000-4000-8000-000000000062';
export const SEED_SECONDARY_CLASS_ID = 'b1a7c0de-0000-4000-8000-000000000063';
export const SEED_SECONDARY_CLASS_NAME = 'Rival Homeroom 1';

export const SEED_SECONDARY_USERS: readonly SeedFixtureUser[] = [
  {
    id: 'b1a7c0de-0000-4000-8000-000000000020',
    email: 'owner2@flatlogic.com',
    namePrefix: USER_NAME_PREFIXES.MR,
    firstName: 'David',
    lastName: 'Martinez',
    role: ROLE_NAMES.OWNER,
    admin: false,
    organization: true,
    school: false,
    campus: false,
  },
  { id: 'b1a7c0de-0000-4000-8000-000000000050', email: 'superintendent2@flatlogic.com', namePrefix: USER_NAME_PREFIXES.DR, firstName: 'Alicia', lastName: 'Reed', role: ROLE_NAMES.SUPERINTENDENT, admin: false, organization: true, school: false, campus: false },
  { id: 'b1a7c0de-0000-4000-8000-000000000051', email: 'principal2@flatlogic.com', namePrefix: USER_NAME_PREFIXES.DR, firstName: 'Henry', lastName: 'Cole', role: ROLE_NAMES.PRINCIPAL, admin: false, organization: true, school: true, campus: false },
  { id: 'b1a7c0de-0000-4000-8000-000000000052', email: 'registrar2@flatlogic.com', namePrefix: USER_NAME_PREFIXES.MS, firstName: 'Maya', lastName: 'Singh', role: ROLE_NAMES.REGISTRAR, admin: false, organization: true, school: true, campus: false },
  { id: 'b1a7c0de-0000-4000-8000-000000000053', email: 'director2@flatlogic.com', namePrefix: USER_NAME_PREFIXES.MR, firstName: 'Ethan', lastName: 'Brooks', role: ROLE_NAMES.DIRECTOR, admin: false, organization: true, school: true, campus: true },
  { id: 'b1a7c0de-0000-4000-8000-000000000054', email: 'office_manager2@flatlogic.com', namePrefix: USER_NAME_PREFIXES.MS, firstName: 'Olivia', lastName: 'Grant', role: ROLE_NAMES.OFFICE_MANAGER, admin: false, organization: true, school: true, campus: true },
  { id: 'b1a7c0de-0000-4000-8000-000000000055', email: 'teacher2@flatlogic.com', namePrefix: USER_NAME_PREFIXES.MRS, firstName: 'Nora', lastName: 'Bennett', role: ROLE_NAMES.TEACHER, admin: false, organization: true, school: true, campus: true },
  { id: 'b1a7c0de-0000-4000-8000-000000000056', email: 'support_staff2@flatlogic.com', namePrefix: USER_NAME_PREFIXES.MR, firstName: 'Caleb', lastName: 'Price', role: ROLE_NAMES.SUPPORT_STAFF, admin: false, organization: true, school: true, campus: true },
  { id: 'b1a7c0de-0000-4000-8000-000000000057', email: 'student2@flatlogic.com', firstName: 'Liam', lastName: 'Martinez', role: ROLE_NAMES.STUDENT, admin: false, organization: true, school: true, campus: true },
  { id: 'b1a7c0de-0000-4000-8000-000000000058', email: 'guardian2@flatlogic.com', namePrefix: USER_NAME_PREFIXES.MRS, firstName: 'Sofia', lastName: 'Martinez', role: ROLE_NAMES.GUARDIAN, admin: false, organization: true, school: true, campus: true },
];

/** Every seeded login user: primary fixtures + the full secondary tenant. */
export const SEED_ALL_USERS: readonly SeedFixtureUser[] = [
  ...SEED_FIXTURE_USERS,
  ...SEED_SECONDARY_USERS,
];
