<script lang="ts">
	import { Button } from '$lib/components/ui/button';
	import * as Dialog from '$lib/components/ui/dialog';
	import { Input } from '$lib/components/ui/input';
	import { Label } from '$lib/components/ui/label';
	import { transportersApi, type Transporter } from '$lib/api/dashboard/a76/transporters';
	import { companyStore } from '$lib/stores/company.svelte';

	let {
		open = $bindable(false),
		item = null,
		onSuccess
	}: {
		open: boolean;
		item?: Transporter | null;
		onSuccess?: () => void;
	} = $props();

	// Determinar si es modo edición o creación
	const isEdit = $derived(!!item);
	const title = $derived(isEdit ? 'Editar Transportista' : 'Nuevo Transportista');

	// Estado del formulario
	let formData = $state<Transporter>({
		transporter_key: '',
		name: '',
		short_name: '',
		responsible: '',
		rfc: '',
		streets: '',
		postal_code: '',
		city: '',
		state: '',
		country: '',
		loader_code: '',
		caat_code: '',
		transport_code: '',
		transport_interface_type: '',
		ftp_server: '',
		ftp_user: '',
		ftp_password: '',
		ftp_directory: '',
		filler_code: ''
	});

	let loading = $state(false);
	let error = $state<string | null>(null);

	// Resetear formulario cuando cambia el item
	$effect(() => {
		if (item) {
			formData = { ...item };
		} else {
			formData = {
				transporter_key: '',
				name: '',
				short_name: '',
				responsible: '',
				rfc: '',
				streets: '',
				postal_code: '',
				city: '',
				state: '',
				country: '',
				loader_code: '',
				caat_code: '',
				transport_code: '',
				transport_interface_type: '',
				ftp_server: '',
				ftp_user: '',
				ftp_password: '',
				ftp_directory: '',
				filler_code: ''
			};
		}
	});

	async function handleSubmit() {
		error = null;
		loading = true;

		try {
			const companyId = companyStore.activeCompany?.id;
			if (!companyId) {
				throw new Error('No hay una compañía seleccionada');
			}

			// Validación básica
			if (!formData.transporter_key.trim()) {
				throw new Error('La clave es requerida');
			}

			let response;
			if (isEdit && item) {
				response = await transportersApi.update(item.transporter_key, formData, companyId);
			} else {
				response = await transportersApi.create(formData, companyId);
			}

			if (response.error) {
				throw new Error(response.error);
			}

			// Cerrar diálogo y notificar éxito
			open = false;
			if (onSuccess) {
				onSuccess();
			}
		} catch (e) {
			error = e instanceof Error ? e.message : 'Error al guardar el transportista';
		} finally {
			loading = false;
		}
	}

	function handleCancel() {
		open = false;
		error = null;
	}
</script>

<Dialog.Root bind:open>
	<Dialog.Content class="max-h-[90vh] overflow-y-auto sm:max-w-5xl">
		<Dialog.Header>
			<Dialog.Title>{title}</Dialog.Title>
			<Dialog.Description>
				{isEdit
					? 'Modifica los datos del transportista'
					: 'Completa los datos para crear un nuevo transportista'}
			</Dialog.Description>
		</Dialog.Header>

		<form
			onsubmit={(e) => {
				e.preventDefault();
				handleSubmit();
			}}
			class="space-y-6"
		>
			{#if error}
				<div class="rounded-md bg-destructive/15 p-3 text-sm text-destructive">
					{error}
				</div>
			{/if}

			<div class="grid grid-cols-1 gap-6 md:grid-cols-2">
				<!-- Información General -->
				<section class="space-y-4">
					<h3 class="border-b pb-2 text-sm font-semibold">Información General</h3>

					<div class="grid gap-2">
						<Label for="transporter_key">Clave <span class="text-destructive">*</span></Label>
						<Input
							id="transporter_key"
							bind:value={formData.transporter_key}
							disabled={isEdit}
							required
							maxlength={23}
						/>
					</div>

					<div class="grid gap-2">
						<Label for="name">Nombre / Razón Social</Label>
						<Input id="name" bind:value={formData.name} maxlength={256} />
					</div>

					<div class="grid gap-2">
						<Label for="short_name">Nombre Corto</Label>
						<Input
							id="short_name"
							bind:value={formData.short_name}
							maxlength={10}
							placeholder="Máx. 10 car."
						/>
					</div>

					<div class="grid gap-2">
						<Label for="rfc">RFC</Label>
						<Input id="rfc" bind:value={formData.rfc} />
					</div>

					<div class="grid gap-2">
						<Label for="responsible">Responsable</Label>
						<Input id="responsible" bind:value={formData.responsible} />
					</div>
				</section>

				<!-- Códigos y Transporte -->
				<section class="space-y-4">
					<h3 class="border-b pb-2 text-sm font-semibold">Códigos de Transporte</h3>

					<div class="grid gap-2">
						<Label for="caat_code">Código CAAT</Label>
						<Input id="caat_code" bind:value={formData.caat_code} />
					</div>

					<div class="grid gap-2">
						<Label for="transport_code">Código de Transporte</Label>
						<Input
							id="transport_code"
							bind:value={formData.transport_code}
							maxlength={8}
							placeholder="Máx. 8 car."
						/>
					</div>

					<div class="grid gap-2">
						<Label for="loader_code">Código Cargador</Label>
						<Input
							id="loader_code"
							bind:value={formData.loader_code}
							maxlength={9}
							placeholder="Máx. 9 car."
						/>
					</div>

					<div class="grid gap-2">
						<Label for="transport_interface_type">Tipo Interfaz</Label>
						<Input
							id="transport_interface_type"
							bind:value={formData.transport_interface_type}
							maxlength={20}
						/>
					</div>

					<div class="grid gap-2">
						<Label for="filler_code">Código Relleno</Label>
						<Input id="filler_code" bind:value={formData.filler_code} maxlength={20} />
					</div>
				</section>

				<!-- Dirección -->
				<section class="space-y-4">
					<h3 class="border-b pb-2 text-sm font-semibold">Dirección</h3>

					<div class="grid gap-2">
						<Label for="streets">Calle y Número</Label>
						<Input id="streets" bind:value={formData.streets} />
					</div>

					<div class="grid grid-cols-2 gap-4">
						<div class="grid gap-2">
							<Label for="city">Ciudad</Label>
							<Input id="city" bind:value={formData.city} maxlength={30} />
						</div>
						<div class="grid gap-2">
							<Label for="state">Estado</Label>
							<Input id="state" bind:value={formData.state} maxlength={30} />
						</div>
					</div>

					<div class="grid grid-cols-2 gap-4">
						<div class="grid gap-2">
							<Label for="country">País</Label>
							<Input
								id="country"
								bind:value={formData.country}
								maxlength={3}
								placeholder="MEX / USA"
							/>
						</div>
						<div class="grid gap-2">
							<Label for="postal_code">C.P.</Label>
							<Input id="postal_code" bind:value={formData.postal_code} />
						</div>
					</div>
				</section>

				<!-- Configuración FTP -->
				<section class="space-y-4">
					<h3 class="border-b pb-2 text-sm font-semibold">Configuración FTP</h3>

					<div class="grid gap-2">
						<Label for="ftp_server">Servidor FTP</Label>
						<Input id="ftp_server" bind:value={formData.ftp_server} />
					</div>

					<div class="grid grid-cols-2 gap-4">
						<div class="grid gap-2">
							<Label for="ftp_user">Usuario</Label>
							<Input id="ftp_user" bind:value={formData.ftp_user} />
						</div>
						<div class="grid gap-2">
							<Label for="ftp_password">Contraseña</Label>
							<Input id="ftp_password" type="password" bind:value={formData.ftp_password} />
						</div>
					</div>

					<div class="grid gap-2">
						<Label for="ftp_directory">Directorio</Label>
						<Input id="ftp_directory" bind:value={formData.ftp_directory} />
					</div>
				</section>
			</div>

			<Dialog.Footer>
				<Button type="button" variant="outline" onclick={handleCancel} disabled={loading}>
					Cancelar
				</Button>
				<Button type="submit" disabled={loading}>
					{loading ? 'Guardando...' : isEdit ? 'Actualizar' : 'Crear'}
				</Button>
			</Dialog.Footer>
		</form>
	</Dialog.Content>
</Dialog.Root>
