import { PrismaClient } from '@prisma/client';
import * as bcrypt from 'bcryptjs';

const prisma = new PrismaClient();

async function main() {
  console.log('🌱 Seeding database...');

  const hash = await bcrypt.hash('senha123', 12);

  const tenant = await prisma.tenant.upsert({
    where: { slug: 'demo' },
    update: {},
    create: {
      name: 'Empresa Demo',
      slug: 'demo',
      plan: 'PRO',
      maxClients: 500,
      maxUsers: 10,
      users: {
        create: {
          name: 'Admin Demo',
          email: 'admin@demo.com',
          passwordHash: hash,
          role: 'ADMIN',
        },
      },
    },
    include: { users: true },
  });

  const admin = tenant.users[0];

  const clients = await Promise.all([
    prisma.client.upsert({
      where: { id: 'seed-client-1' },
      update: {},
      create: {
        id: 'seed-client-1',
        tenantId: tenant.id,
        name: 'João Silva',
        phone: '11999990001',
        whatsapp: '11999990001',
        address: 'Rua das Flores, 123',
        city: 'São Paulo',
        state: 'SP',
        type: 'RESIDENTIAL',
      },
    }),
    prisma.client.upsert({
      where: { id: 'seed-client-2' },
      update: {},
      create: {
        id: 'seed-client-2',
        tenantId: tenant.id,
        name: 'Empresa ABC Ltda',
        phone: '11999990002',
        address: 'Av. Paulista, 1000',
        city: 'São Paulo',
        state: 'SP',
        type: 'COMMERCIAL',
      },
    }),
    prisma.client.upsert({
      where: { id: 'seed-client-3' },
      update: {},
      create: {
        id: 'seed-client-3',
        tenantId: tenant.id,
        name: 'Maria Santos',
        phone: '11999990003',
        city: 'Campinas',
        state: 'SP',
        type: 'RESIDENTIAL',
      },
    }),
  ]);

  const order = await prisma.serviceOrder.upsert({
    where: { tenantId_number: { tenantId: tenant.id, number: 1 } },
    update: {},
    create: {
      tenantId: tenant.id,
      clientId: clients[0].id,
      technicianId: admin.id,
      number: 1,
      description: 'Manutenção preventiva do ar-condicionado split 12000 BTUs',
      serviceType: 'Manutenção Preventiva',
      status: 'DONE',
      value: 250.00,
      completedAt: new Date(),
    },
  });

  const overdueDate = new Date();
  overdueDate.setMonth(overdueDate.getMonth() - 2);

  await prisma.reminder.upsert({
    where: { id: 'seed-reminder-1' },
    update: {},
    create: {
      id: 'seed-reminder-1',
      tenantId: tenant.id,
      clientId: clients[0].id,
      orderId: order.id,
      nextMaintenanceDate: overdueDate,
      recurrenceMonths: 6,
      status: 'OVERDUE',
    },
  });

  const todayDate = new Date();
  await prisma.reminder.upsert({
    where: { id: 'seed-reminder-2' },
    update: {},
    create: {
      id: 'seed-reminder-2',
      tenantId: tenant.id,
      clientId: clients[1].id,
      nextMaintenanceDate: todayDate,
      recurrenceMonths: 3,
      status: 'PENDING',
    },
  });

  console.log('✅ Seed complete!');
  console.log('📧 Login: admin@demo.com');
  console.log('🔑 Senha: senha123');
}

main()
  .catch(console.error)
  .finally(() => prisma.$disconnect());
