// Auto-generated by create-backlist v5.1

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "<%= dbProvider || 'postgresql' %>"
  url      = env("DATABASE_URL")
}

<%
function sanitizeFieldName(name) {
  const s = String(name || '').trim().replace(/[^a-zA-Z0-9_]/g, '_');
  if (!s) return 'field';
  if (/^[0-9]/.test(s)) return '_' + s;
  return s;
}
%>

<% modelsToGenerate.forEach(model => { %>
model <%= model.name %> {
  <% if (useIntId) { %>
  id        Int      @id @default(autoincrement())
  <% } else { %>
  id        String   @id @default(cuid())
  <% } %>

  <% model.fields.forEach(field => { 
    const fname = sanitizeFieldName(field.name);
    const lower = String(fname).toLowerCase();

    let prismaType = 'String';
    if (field.type === 'Number') prismaType = 'Int';
    if (field.type === 'Boolean') prismaType = 'Boolean';

    // money/decimal heuristic
    if (field.type === 'Number' && (lower.includes('price') || lower.includes('amount') || lower.includes('total'))) {
      prismaType = 'Decimal';
    }

    const optional = field.isOptional ? '?' : '';
    const unique = field.isUnique ? ' @unique' : '';
  %>
  <%= fname.padEnd(16) %> <%= prismaType %><%= optional %><%= unique %>
  <% }); %>

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}
<% }); %>