'use client'
import { use } from 'react'
import { AppShell } from '@/components/layout/AppShell'
import { OperationForm } from '@/components/operations/OperationForm'
import { useOperationsStore } from '@/store/operationsStore'

export default function EditarOperacionPage({ params }: { params: Promise<{ id: string }> }) {
  const { id } = use(params)
  const op = useOperationsStore(s => s.getById(id))

  const initialData = op ? {
    property_address:      op.property_address ?? '',
    property_locality:     op.property_locality ?? '',
    property_type:         op.property_type ?? '',
    property_observations: op.property_observations ?? '',
    owner_name:            op.owner_name ?? '',
    owner_dni:             op.owner_dni ?? '',
    owner_phone:           op.owner_phone ?? '',
    owner_email:           op.owner_email ?? '',
    tenant_name:           op.tenant_name ?? '',
    tenant_dni:            op.tenant_dni ?? '',
    tenant_phone:          op.tenant_phone ?? '',
    tenant_email:          op.tenant_email ?? '',
    guarantee_type:        op.guarantee_type ?? '',
    guarantee_company:     op.guarantee_company ?? '',
    guarantee_observations: op.guarantee_observations ?? '',
    entry_date:            op.entry_date ?? '',
    start_date:            op.start_date ?? '',
    end_date:              op.end_date ?? '',
    initial_value:         op.initial_value?.toString() ?? '',
    adjustment_type:       op.adjustment_type ?? '',
    adjustment_frequency:  op.adjustment_frequency ?? '',
    assigned_to:           op.assigned_to ?? '',
  } : undefined

  return (
    <AppShell>
      <OperationForm
        initialData={initialData}
        initialOperationType={op?.operation_type}
        editId={id}
        editCode={op?.internal_code}
      />
    </AppShell>
  )
}
