import type { CollectionConfig } from 'payload'

import { isSuperAdminAccess } from '@/access/isSuperAdmin'
import { updateAndDeleteAccess } from './access/updateAndDelete'

/**
 * Tenants Collection - Represents Care Homes
 *
 * Each tenant is an elderly care home with their own:
 * - Residents
 * - Meal orders
 * - Staff (caregivers, kitchen staff)
 */
export const Tenants: CollectionConfig = {
  slug: 'tenants',
  labels: {
    singular: 'Care Home',
    plural: 'Care Homes',
  },
  access: {
    create: isSuperAdminAccess,
    delete: updateAndDeleteAccess,
    read: ({ req }) => Boolean(req.user),
    update: updateAndDeleteAccess,
  },
  admin: {
    useAsTitle: 'name',
    description: 'Manage care homes in the system',
    defaultColumns: ['name', 'slug', 'phone'],
  },
  fields: [
    {
      name: 'name',
      type: 'text',
      required: true,
      admin: {
        description: 'Care home name',
      },
    },
    {
      name: 'slug',
      type: 'text',
      required: true,
      index: true,
      unique: true,
      admin: {
        description: 'URL-friendly identifier (e.g., sunny-meadows)',
      },
    },
    {
      name: 'domain',
      type: 'text',
      admin: {
        description: 'Optional custom domain (e.g., sunny-meadows.localhost)',
      },
    },
    {
      name: 'address',
      type: 'textarea',
      admin: {
        description: 'Physical address of the care home',
      },
    },
    {
      name: 'phone',
      type: 'text',
      admin: {
        description: 'Contact phone number',
      },
    },
  ],
}
