import {
  pgTable,
  serial,
  varchar,
  timestamp,
  boolean,
  text,
  integer,
} from 'drizzle-orm/pg-core'
import { relations, sql } from 'drizzle-orm'
import { createInsertSchema, createSelectSchema } from 'drizzle-zod'
import { z } from 'zod'
import resourceLanguagesTable from '../relations/resource-language.relation'
import resourceSubjectsTable from '../relations/resource-subject.relation'
import resourceEducationalStagesTable from '../relations/resource-educational-stage.relation'
import userTable from './user.schema'
import objectTypeTable from './object-type.schema'
import licenseTable from './license.schema'
import resourceStatsTable from './resource-stats.schema'
import resourceEnum from './resourceEnum.schema'

//por padrao active é false, só é true quando o recurso é aprovado
const resourceTable = pgTable('resources', {
  id: serial('id').primaryKey()
    .notNull()
    .unique(),
  state: resourceEnum('state').notNull().default('draft'),
  name: varchar('name', { length: 255 })
    .notNull(),
  author: varchar('author', { length: 255 })
    .notNull(),
  link: varchar('link', { length: 255 }),
  thumbnail: varchar('thumbnail', { length: 255 })
    .notNull(),
  description: text('description'),
  active: boolean('active')
    .notNull()
    .default(false),
  created_at: timestamp('created_at', { mode: 'string' })
    .notNull()
    .defaultNow(),
  updated_at: timestamp('updated_at', { mode: 'string' })
    .notNull()
    .defaultNow()
    .$onUpdate(() => sql`current_timestamp`),
  published_at: timestamp('published_at', { mode: 'string' }),
  submitted_at: timestamp('submitted_at', { mode: 'string' }),
  deleted_at: timestamp('deleted_at', { mode: 'string' }),
  user_id: integer('user_id')
    .notNull()
    .references(() => userTable.id, { onDelete: 'cascade' }),
  resource_stats_id: integer('resource_stats_id')
    .notNull()
    .unique()
    .references(() => resourceStatsTable.id, { onDelete: 'cascade' }),
  object_type_id: integer('object_type_id')
    .notNull()
    .references(() => objectTypeTable.id, { onDelete: 'cascade' }),
  license_id: integer('license_id')
    .notNull()
    .references(() => licenseTable.id, { onDelete: 'cascade' }),
})


export const resourceRelations = relations(resourceTable, ({ many }) => ({
  resourceLanguages: many(resourceLanguagesTable),
  resourceSubjects: many(resourceSubjectsTable),
  resourceEducatinalStages: many(resourceEducationalStagesTable)
})
)


const resourceModelSchema = createSelectSchema(resourceTable)
const resourceDtoSchema = resourceModelSchema.omit({})
const resourceInputSchema = createInsertSchema(resourceTable)
  .partial()
  .extend({
    resource_stats_id: z.number().optional(),
  })
  .omit({
    active: true
  })

const resourceUpdateSchema = resourceInputSchema
  .partial()
  .required({ id: true })


export type ResourceModel = z.infer<typeof resourceModelSchema>
export type ResourceDto = z.infer<typeof resourceDtoSchema>
export type ResourceInput = z.infer<typeof resourceInputSchema>
export type ResourceUpdate = z.infer<typeof resourceUpdateSchema>

export const resourceSchema = {
  model: resourceModelSchema,
  dto: resourceDtoSchema,
  input: resourceInputSchema,
  update: resourceUpdateSchema,
}

export default resourceTable