import { defineRelations } from 'drizzle-orm'
import * as schema from './schema'

export const relations = defineRelations(schema, (relation) => {
  return {
    users: {
      oauthAccounts: relation.many.oauthAccounts({
        from: relation.users.id,
        to: relation.oauthAccounts.userId
      }),

      userEquipment: relation.many.userEquipment({
        from: relation.users.id,
        to: relation.userEquipment.userId
      }),

      packingLists: relation.many.packingLists({
        from: relation.users.id,
        to: relation.packingLists.userId
      }),

      contributions: relation.many.contributions({
        from: relation.users.id,
        to: relation.contributions.userId
      }),

      createdItems: relation.many.equipmentItems({
        from: relation.users.id,
        to: relation.equipmentItems.createdBy
      })
    },

    oauthProviders: {
      oauthAccounts: relation.many.oauthAccounts({
        from: relation.oauthProviders.id,
        to: relation.oauthAccounts.providerId
      })
    },

    equipmentGroups: {},

    equipmentCategories: {
      items: relation.many.equipmentItems({
        from: relation.equipmentCategories.id,
        to: relation.equipmentItems.categoryId
      }),

      properties: relation.many.categoryProperties({
        from: relation.equipmentCategories.id,
        to: relation.categoryProperties.categoryId
      })
    },

    brands: {
      items: relation.many.equipmentItems({
        from: relation.brands.id,
        to: relation.equipmentItems.brandId
      })
    },

    equipmentItems: {
      category: relation.one.equipmentCategories({
        from: relation.equipmentItems.categoryId,
        to: relation.equipmentCategories.id
      }),

      brand: relation.one.brands({
        from: relation.equipmentItems.brandId,
        to: relation.brands.id
      }),

      creator: relation.one.users({
        from: relation.equipmentItems.createdBy,
        to: relation.users.id
      }),

      propertyValues: relation.many.itemPropertyValues({
        from: relation.equipmentItems.id,
        to: relation.itemPropertyValues.itemId
      }),

      userEquipment: relation.many.userEquipment({
        from: relation.equipmentItems.id,
        to: relation.userEquipment.itemId
      }),

      images: relation.many.equipmentItemImages({
        from: relation.equipmentItems.id,
        to: relation.equipmentItemImages.itemId
      })
    },

    categoryProperties: {
      category: relation.one.equipmentCategories({
        from: relation.categoryProperties.categoryId,
        to: relation.equipmentCategories.id
      }),

      enumOptions: relation.many.propertyEnumOptions({
        from: relation.categoryProperties.id,
        to: relation.propertyEnumOptions.propertyId
      }),

      values: relation.many.itemPropertyValues({
        from: relation.categoryProperties.id,
        to: relation.itemPropertyValues.propertyId
      })
    },

    propertyEnumOptions: {
      property: relation.one.categoryProperties({
        from: relation.propertyEnumOptions.propertyId,
        to: relation.categoryProperties.id
      })
    },

    itemPropertyValues: {
      item: relation.one.equipmentItems({
        from: relation.itemPropertyValues.itemId,
        to: relation.equipmentItems.id
      }),

      property: relation.one.categoryProperties({
        from: relation.itemPropertyValues.propertyId,
        to: relation.categoryProperties.id
      })
    },

    userEquipment: {
      user: relation.one.users({
        from: relation.userEquipment.userId,
        to: relation.users.id
      }),

      item: relation.one.equipmentItems({
        from: relation.userEquipment.itemId,
        to: relation.equipmentItems.id
      }),

      packingListEntries: relation.many.packingListEntries({
        from: relation.userEquipment.id,
        to: relation.packingListEntries.userEquipmentId
      })
    },

    packingLists: {
      user: relation.one.users({
        from: relation.packingLists.userId,
        to: relation.users.id
      }),

      entries: relation.many.packingListEntries({
        from: relation.packingLists.id,
        to: relation.packingListEntries.packingListId
      })
    },

    packingListEntries: {
      packingList: relation.one.packingLists({
        from: relation.packingListEntries.packingListId,
        to: relation.packingLists.id
      }),

      userEquipment: relation.one.userEquipment({
        from: relation.packingListEntries.userEquipmentId,
        to: relation.userEquipment.id
      })
    },

    contributions: {
      user: relation.one.users({
        from: relation.contributions.userId,
        to: relation.users.id
      })
    },

    equipmentItemImages: {
      item: relation.one.equipmentItems({
        from: relation.equipmentItemImages.itemId,
        to: relation.equipmentItems.id
      })
    }
  }
})
